Java while 循环没有按预期工作
Java while loop doesn't work as intended
public static void main(String[] args) {
boolean run = true;
while (run) {
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to MetricMan - the best converter in town!");
System.out.println("Choose and option:");
System.out.println("A) Fahrenheit to Celsius");
System.out.println("B) Celcius to Fahrenheit");
System.out.println("C) Miles to Kilometers");
System.out.println("D) Kilometers to Miles");
System.out.println("E) Inches to Centimeters");
System.out.println("F) Centimeters to Inches");
System.out.println("X) Quit the program");
System.out.print("Your choice > ");
String choice = kb.nextLine();
double output; int input;
System.out.print("Enter the number of ");
switch (choice) { // do actions according to the user's choice. convert and display
case "A":
System.out.print("Farenheit > ");
input = kb.nextInt();
output = convertFtoC(input);
System.out.println(input+" farenheit is "+output+" celsius");
break;
case "B":
System.out.print("Celsius > ");
input = kb.nextInt();
output = convertCtoF(input);
System.out.println(input+" celsius is "+output+" farenheit");
break;
case "C":
System.out.print("miles > ");
input = kb.nextInt();
output = convertMtoK(input);
System.out.println(input+" miles is "+output+" kilometres");
break;
case "D":
System.out.print("kilometres > ");
input = kb.nextInt();
output = convertKtoM(input);
System.out.println(input+" kilometres is "+output+" miles");
break;
case "E":
System.out.print("inches > ");
input = kb.nextInt();
output = convertItoC(input);
System.out.println(input+" inches is "+output+" centimeters");
break;
case "F":
System.out.print("centimeters > ");
input = kb.nextInt();
output = convertCtoI(input);
System.out.println(input+" centimeters is "+output+" inches");
break;
case "X":
System.out.println(); System.out.println();
System.out.println("May the odds be ever metric!");
run = false;
break;
}
System.out.println();
kb.nextLine();
}
} // end of main()
这是我的主要方法。从代码中可以看出,当输入 X 时,代码应该立即停止 运行,但它的实际输出是
...
Your choice > X
Enter the number of
May the odds be ever metric!
在实际预期的输出之前有一个 "enter the number of ",我认为没有理由在那里。谁能帮忙解决这个问题?
每次执行循环时都会出现该行。解决此问题的一种方法是在除 X 之外的每个 case 语句中进行调用。
在你的代码中
System.out.print("Enter the number of ");
switch (choice) { // do actions according to the user's choice. convert and display
所以你基本上是在 switch 之前打印这个,所以输出是预期的,你可能想调用内部案例而不是外部 switch,当它 x 时,就退出。
你在每种情况下都在做 System.out.print("Enter the number of ");
。所以这就是为什么它在你输入 X
时打印,所以试着把 System.out.print("Enter the number of ");
放在 if(check if string entered is not X)
里面,如下所示
if(!choice.equals("X")){
System.out.print("Enter the number of ");
}
或
将System.out.print("Enter the number of ");
放入@Tanmoy
所指的每个开关盒中
public static void main(String[] args) {
boolean run = true;
while (run) {
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to MetricMan - the best converter in town!");
System.out.println("Choose and option:");
System.out.println("A) Fahrenheit to Celsius");
System.out.println("B) Celcius to Fahrenheit");
System.out.println("C) Miles to Kilometers");
System.out.println("D) Kilometers to Miles");
System.out.println("E) Inches to Centimeters");
System.out.println("F) Centimeters to Inches");
System.out.println("X) Quit the program");
System.out.print("Your choice > ");
String choice = kb.nextLine();
double output; int input;
System.out.print("Enter the number of ");
switch (choice) { // do actions according to the user's choice. convert and display
case "A":
System.out.print("Farenheit > ");
input = kb.nextInt();
output = convertFtoC(input);
System.out.println(input+" farenheit is "+output+" celsius");
break;
case "B":
System.out.print("Celsius > ");
input = kb.nextInt();
output = convertCtoF(input);
System.out.println(input+" celsius is "+output+" farenheit");
break;
case "C":
System.out.print("miles > ");
input = kb.nextInt();
output = convertMtoK(input);
System.out.println(input+" miles is "+output+" kilometres");
break;
case "D":
System.out.print("kilometres > ");
input = kb.nextInt();
output = convertKtoM(input);
System.out.println(input+" kilometres is "+output+" miles");
break;
case "E":
System.out.print("inches > ");
input = kb.nextInt();
output = convertItoC(input);
System.out.println(input+" inches is "+output+" centimeters");
break;
case "F":
System.out.print("centimeters > ");
input = kb.nextInt();
output = convertCtoI(input);
System.out.println(input+" centimeters is "+output+" inches");
break;
case "X":
System.out.println(); System.out.println();
System.out.println("May the odds be ever metric!");
run = false;
break;
}
System.out.println();
kb.nextLine();
}
} // end of main()
这是我的主要方法。从代码中可以看出,当输入 X 时,代码应该立即停止 运行,但它的实际输出是
...
Your choice > X
Enter the number of
May the odds be ever metric!
在实际预期的输出之前有一个 "enter the number of ",我认为没有理由在那里。谁能帮忙解决这个问题?
每次执行循环时都会出现该行。解决此问题的一种方法是在除 X 之外的每个 case 语句中进行调用。
在你的代码中
System.out.print("Enter the number of ");
switch (choice) { // do actions according to the user's choice. convert and display
所以你基本上是在 switch 之前打印这个,所以输出是预期的,你可能想调用内部案例而不是外部 switch,当它 x 时,就退出。
你在每种情况下都在做 System.out.print("Enter the number of ");
。所以这就是为什么它在你输入 X
时打印,所以试着把 System.out.print("Enter the number of ");
放在 if(check if string entered is not X)
里面,如下所示
if(!choice.equals("X")){
System.out.print("Enter the number of ");
}
或
将System.out.print("Enter the number of ");
放入@Tanmoy