gpt4 book ai didi

java - Java 新手,Do-While 循环问题

转载 作者:行者123 更新时间:2023-12-01 07:03:43 24 4
gpt4 key购买 nike

我对 Java 比较陌生,在运行这个 do-while 循环时遇到一些麻烦。

问题1

Write a program that allows the user to convert a temperature given in degrees from either Celsius to Fahrenheit or Fahrenheit to Celsius. Use the following formulas: Degrees_C = 5 (Degrees_F − 32) / 9 Degrees_F = (9 (Degrees_C) / 5) + 32 Prompt the user to enter a temperature and either a C or c for Celsius or an F or f for Fahrenheit. Convert the temperature to Fahrenheit if Celsius is entered, or to Celsius if Fahrenheit is entered. Display the result in a readable format. If anything other than C, c, F, or f is entered, print an error message and stop.

问题2

Let’s continue Problem 1, but use a loop so the user can convert other temperatures. If the user enters a letter other than C or F—in either uppercase or lowercase—after a temperature, print an error message and ask the user to reenter a valid selection. After each conversion, ask the user to type Q or q to quit or to press any other key to repeat the loop and perform another conversion

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 循环。

enter image description here

尝试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();
}
}

最佳答案

您有一个拼写错误:

}while(keepGoing = true);

应该是:

}while(keepGoing == true);

或者只是:

}while(keepGoing);

说明

keepGoing = true ,即使在条件内,也会分配 keepGoingtrue并返回其值(在本例中始终为 true)。

另一方面,keepGoing == true要求比较keepGoingtrue值(value)相等。

关于java - Java 新手,Do-While 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32979950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com