Reputation: 425
new to Java as you can tell when you see my question. I want to:
The problems with my code are - Whatever number is hit initially, step 2 from above doesn't appear. It only appears after the second number is entered -When 7 in entered it doesn't actually quit
I'm also new to this site so my apologies if I'm not asking the question properly
Here's the code
class Test
{
public static void main(String[] args)
{
int number;
int index;
int input;
Double conversion;
index = 1;
System.out.println("Enter 1 for Fahrenheit to Celius ");
System.out.println("Enter 2 for Celius to Fahrenheit ");
System.out.println("Enter 3 for Inches to Centimetres ");
System.out.println("Enter 4 for Centimetres to Inches ");
System.out.println("Enter 5 for Pounds to Kg ");
System.out.println("Enter 6 for Kg to Pounds ");
System.out.println("Enter 7 to quit ");
number = EasyIn.getInt();
for (index=1; index <=6; index++)
{
System.out.println("Enter 1 for Fahrenheit to Celsius ");
System.out.println("Enter 2 for Celsius to Fahrenheit ");
System.out.println("Enter 3 for Inches to Centimetres ");
System.out.println("Enter 4 for Centimetres to Inches ");
System.out.println("Enter 5 for Pounds to Kg ");
System.out.println("Enter 6 for Kg to Pounds ");
System.out.println("Enter 7 to quit ");
number = EasyIn.getInt();
if (number == 1)
{
System.out.print("Input an amount to convert to Celsius ");
input = EasyIn.getInt();
conversion = (5.0/9.0) * (input - 32);
System.out.println("Your input to Celsius is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 2)
{
System.out.print("Input an amount to convert to Fahrenheit ");
input = EasyIn.getInt();
conversion = input * 2.8;
System.out.println("Your input to Fahrenheit is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 3)
{
System.out.print("Input an amount to convert to Centimetres ");
input = EasyIn.getInt();
conversion = input * 2.54;
System.out.println("Your input to Centimtres is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 4)
{
System.out.print("Input an amount to convert to Inches ");
input = EasyIn.getInt();
conversion = input * 0.393701;
System.out.println("Your input to Inches is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 5)
{
System.out.print("Input an amount to convert to Kg ");
input = EasyIn.getInt();
conversion = input * 0.453592;
System.out.println("Your input to Kgs is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 6)
{
System.out.print("Input an amount to convert to Pounds ");
input = EasyIn.getInt();
conversion = input * 2.20462;
System.out.println("Your input to pounds is " + conversion);
System.out.println("Press 7 to quit");
}
}
}
}
Upvotes: 0
Views: 217
Reputation: 1469
If I understand the question correctly, the problem is that you're calling
number = EasyIn.getInt();
before your for loop, and then calling it a second time within your for loop. You're not actually doing any processing with the first call to EasyIn.getInt() so you're not seeing any results until the second call.
Pressing 7 is not going to cause your loop to exit because you never handle "7" once it's input.
I think you want something more like this:
while (true) {
//do all your System.out.print() stuff
number = EasyIn.getInt();
// if conditions for 1, 2, 3, 4, 5, and 6
if (number == 7) {
return;
}
}
Upvotes: 0
Reputation: 13483
Instead of me correcting your answer, here is a better solution to your goal:
Instead of a for-loop, use a while-loop. The condition is while the input is not 7.
Tested and works:
int index = 0;
double conversion = 0;
int input = 0;
System.out.println("Enter 1 for Fahrenheit to Celius ");
System.out.println("Enter 2 for Celius to Fahrenheit ");
System.out.println("Enter 3 for Inches to Centimetres ");
System.out.println("Enter 4 for Centimetres to Inches ");
System.out.println("Enter 5 for Pounds to Kg ");
System.out.println("Enter 6 for Kg to Pounds ");
System.out.println("Enter 7 to quit ");
Scanner in = new Scanner(System.in);
index = in.nextInt();
while (index != 7)
{
if (index == 1)
{
System.out.print("Input an amount to convert to Celsius ");
input = in.nextInt();
conversion = (5.0/9.0) * (input - 32);
System.out.println("Your input to Celsius is " + conversion);
System.out.println("Press 7 to quit");
}
if (index == 2)
{
System.out.print("Input an amount to convert to Fahrenheit ");
input = in.nextInt();
conversion = input * 2.8;
System.out.println("Your input to Fahrenheit is " + conversion);
System.out.println("Press 7 to quit");
}
if (index == 3)
{
System.out.print("Input an amount to convert to Centimetres ");
input = in.nextInt();
conversion = input * 2.54;
System.out.println("Your input to Centimtres is " + conversion);
System.out.println("Press 7 to quit");
}
if (index == 4)
{
System.out.print("Input an amount to convert to Inches ");
input = in.nextInt();
conversion = input * 0.393701;
System.out.println("Your input to Inches is " + conversion);
System.out.println("Press 7 to quit");
}
if (index == 5)
{
System.out.print("Input an amount to convert to Kg ");
input = in.nextInt();
conversion = input * 0.453592;
System.out.println("Your input to Kgs is " + conversion);
System.out.println("Press 7 to quit");
}
if (index == 6)
{
System.out.print("Input an amount to convert to Pounds ");
input = in.nextInt();
conversion = input * 2.20462;
System.out.println("Your input to pounds is " + conversion);
System.out.println("Press 7 to quit");
}
index = in.nextInt();
}
Upvotes: 0
Reputation: 3519
You are using index in your four loop, you can modify the loop like that:
while (number != 7) {
// ...
}
You are showing the menu twice in the first iteration. Just one before the loop, and another one inside your loop. You can remove the code for showing the menu before the loop and initialize the value of the loop or use a do-while bucle.
do {
// Show menu code
// Read user action
// Selected action code
} while (number != 7)
Upvotes: 0
Reputation: 202
Couple of things I see. First, you prompt outside your For loop. then you prompt again inside your For loop. That is why it ignores the first number. Get rid of all those System.out.print calls and the GetInt() outside the For loop.
Next, Why are you using a For loop with an index? your code will only loop 6 times then it will end. Every time. No more, no less.
Switch it to a while(true).
To make the quit on seven work.
Add this
if(number == 7)
{
break;
}
Upvotes: 0
Reputation: 1125
public static void main(String[] args){
int number;
int index;
int input;
Double conversion;
do{
System.out.println("Enter 1 for Fahrenheit to Celius ");
System.out.println("Enter 2 for Celius to Fahrenheit ");
System.out.println("Enter 3 for Inches to Centimetres ");
System.out.println("Enter 4 for Centimetres to Inches ");
System.out.println("Enter 5 for Pounds to Kg ");
System.out.println("Enter 6 for Kg to Pounds ");
System.out.println("Enter 7 to quit ");
number = EasyIn.getInt();
if (number == 1)
{
System.out.print("Input an amount to convert to Celsius ");
input = EasyIn.getInt();
conversion = (5.0/9.0) * (input - 32);
System.out.println("Your input to Celsius is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 2)
{
System.out.print("Input an amount to convert to Fahrenheit ");
input = EasyIn.getInt();
conversion = input * 2.8;
System.out.println("Your input to Fahrenheit is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 3)
{
System.out.print("Input an amount to convert to Centimetres ");
input = EasyIn.getInt();
conversion = input * 2.54;
System.out.println("Your input to Centimtres is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 4)
{
System.out.print("Input an amount to convert to Inches ");
input = EasyIn.getInt();
conversion = input * 0.393701;
System.out.println("Your input to Inches is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 5)
{
System.out.print("Input an amount to convert to Kg ");
input = EasyIn.getInt();
conversion = input * 0.453592;
System.out.println("Your input to Kgs is " + conversion);
System.out.println("Press 7 to quit");
}
if (number == 6)
{
System.out.print("Input an amount to convert to Pounds ");
input = EasyIn.getInt();
conversion = input * 2.20462;
System.out.println("Your input to pounds is " + conversion);
System.out.println("Press 7 to quit");
}
}while(number>=1 && number<7);
}
Upvotes: 0
Reputation: 1537
You're setting number
before the loop and then immediately in the loop. That's why it ignores your first value. Remove
number = EasyIn.getInt();
before the for loop.
Also it doesn't quit because you don't tell it what to do when 7 is entered. You need a new if block for that situation:
if(number == 7) {
//bla bla
break;
}
Upvotes: 1
Reputation: 200266
System.out.print("Input an amount to convert to Celsius ");
This output won't reach your display until the System.out
stream is either flushed explicitly or a newline is printed to it. You go on to expect user input before either happens, therefore you don't see the prompt.
This is so because in general, output streams are buffered to optimize performance when much data is sent over them. PrintStream
has autoflush behavior, which makes this issue invisible most of the time. It works by flushing at each end-of-line mark, which you don't send with print
.
Upvotes: 3