I write a java program that allow people to convert Fahrenheit to Celsius or Celsius to Fahrenheit repeatedly. Right now the code runs fine, but if I delete the two code input=keyboardnextLine() in the two if statement, the code only runs once and stops.
I don't know where I do wrong. I think the two input=keyboardnextLine() in the if statements are useless. please help me, it confuses me a lot!
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
System.out.println("Enter\"f\" or \"F\" to convert fahrenheit to centigrade");
System.out.println("Enter\"c\" or \"C\" to convert centigrade to fahrenheit");
System.out.println("Enter someting else to quit");
Scanner keyboard = new Scanner (System.in);
String input = keyboard.nextLine();
double fah, cel;
while ((input.equalsIgnoreCase("f")) || (input.equalsIgnoreCase("c"))) {
if (input.equalsIgnoreCase("f")) {
System.out.println("Enter the temperature in fahrenheit. I will tell you the centigrade.");
fah = keyboard.nextDouble();
cel = 5*(fah - 32) / 9;
System.out.println(fah+ " in centigrade is " + cel + " in fahrenheit.");
System.out.println(" ");
System.out.println("Enter\"f\" or \"F\" to convert fahrenheit to centigrade");
System.out.println("Enter\"c\" or \"C\" to convert centigrade to fahrenheit");
System.out.println("Enter someting else to quit");
input = keyboard.nextLine();
}
if (input.equalsIgnoreCase("c")) {
System.out.println("Enter the temperature in centigrade. I will tell you the fahrenheit.");
cel = keyboard.nextDouble();
fah = 9 * cel / 5 + 32;
System.out.println(cel + " in centigrade is " + fah + " in fahrenheit.");
System.out.println(" ");
System.out.println("Enter\"f\" or \"F\" to convert fahrenheit to centigrade");
System.out.println("Enter\"c\" or \"C\" to convert centigrade to fahrenheit");
System.out.println("Enter someting else to quit");
input = keyboard.nextLine();
}
input = keyboard.nextLine();
}
System.exit(0);
}
}
nextLineswallows the newline, et al, associated with the user input.