【发布时间】:2016-01-03 23:02:33
【问题描述】:
我对 Java 比较陌生,我在运行这个 do-while 循环时遇到了一些麻烦。
问题 1
编写一个程序,允许用户转换给定的温度 摄氏度到华氏度或华氏度到摄氏度。 使用以下公式:Degrees_C = 5 (Degrees_F − 32) / 9 Degrees_F = (9 (Degrees_C) / 5) + 32 提示用户输入 温度,C 或 c 表示摄氏温度,F 或 f 表示 华氏度。如果摄氏度是,将温度转换为华氏度 输入,如果输入华氏温度,则为摄氏温度。显示结果 一种可读的格式。如果输入 C、c、F 或 f 以外的任何内容, 打印错误消息并停止。
问题 2
让我们继续问题 1,但使用循环,以便用户可以转换其他 温度。如果用户输入 C 或 F 以外的字母——在任一 大写或小写—温度后,打印错误消息并 要求用户重新输入有效的选择。每次转换后,询问 用户键入 Q 或 q 退出或按任何其他键重复 循环并执行另一个转换
public class Lab_4 {
public static void main(String arg[]) {
Scanner input = new Scanner(System.in);
double F; //Fahrenheit
double C; //Celsius
String method;
boolean keepGoing = true;
do {
System.out.println("Choose a method: ");
System.out.println("(F) Fahrenheit to Celsius. ");
System.out.println("(C) Celsius to Fahrenheit. ");
System.out.println("(Q) Exit the loop. ");
method = input.nextLine();
if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
System.out.println("Method F");
System.out.println("Enter the temperature in Fahrenheit: ");
F = input.nextDouble();
C = 5 * (F - 32) / 9;
System.out.println("Temperature in Celsius: " + C); }
if (method.charAt(0) == 'c' || method.charAt(0) == 'C') {
System.out.println("Method C");
System.out.println("Enter the temperature in Celsius: ");
C = input.nextDouble();
F = (9 * C / 5) + 32;
System.out.println("Temperature in Fahrenheit: " + F); }
if (method.charAt(0)== 'q' || method.charAt(0)== 'Q') {
keepGoing = false;
System.out.println("Exiting the loop! "); }
else {
//if index 0 doesn't equal to C, F, Q, it's out of range.
System.out.println("Method is out of range! ");
}
}while(keepGoing = true);
input.close();
}
}
循环将继续进行,直到我输入 Q 或 q 退出。所以我必须输入 Q 才能退出循环,但在我得到转换值后立即收到一条错误消息,它根本没有通过循环。该程序只是不通过while循环。
尝试 2,仍然错误
package Lab_4;
import java.util.Scanner;
public class Lab_4 {
public static void main(String arg[]) {
Scanner input = new Scanner(System.in);
double F; //Fahrenheit
double C; //Celsius
String method;
boolean keepGoing = true;
do {
System.out.println("Choose a method: ");
System.out.println("(F) Fahrenheit to Celsius. ");
System.out.println("(C) Celsius to Fahrenheit. ");
System.out.println("(Q) Exit the loop. ");
method = input.nextLine();
if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
System.out.println("Method F");
System.out.println("Enter the temperature in Fahrenheit: ");
F = input.nextDouble();
C = 5 * (F - 32) / 9;
System.out.println("Temperature in Celsius: " + C); }
else if (method.charAt(0) == 'c' || method.charAt(0) == 'C') {
System.out.println("Method C");
System.out.println("Enter the temperature in Celsius: ");
C = input.nextDouble();
F = (9 * C / 5) + 32;
System.out.println("Temperature in Fahrenheit: " + F); }
else if (method.charAt(0)== 'q' || method.charAt(0)== 'Q') {
keepGoing = false;
System.out.println("Exiting the loop! "); }
else {
//if index 0 doesn't equal to C, F, Q, it's out of range.
System.out.println("Method is out of range! ");
}
}while(keepGoing);
input.close();
}
}
【问题讨论】:
-
尝试将第二个和第三个 ifs 更改为
else if -
如果放 if else 而不是 if 并修复 while(keepGoing),但程序仍然没有读取,因为 Java 显然无法读取字符串索引(超出范围)。
标签: java loops while-loop boolean