我对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 …Run Code Online (Sandbox Code Playgroud)