在这个程序中,我必须一次又一次地在循环中输入温度。当我输入温度一旦它显示正确的显示,但当我再次输入温度时,程序自行终止。
代码是:
import java.util.Scanner;
public class CheckTemperature
{
public static void main(String [] args)
{
final double TEMPERATURE = 102.5;
double thermostat;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the substance's temperature: ");
thermostat = keyboard.nextDouble();
if(thermostat > TEMPERATURE)
{
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat < TEMPERATURE)
{
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat == TEMPERATURE)
{
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else
{
System.out.println("Enter the correct temperature value ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
}
}
答案 0 :(得分:5)
这是因为你不在循环中。你问温度一次在顶部。 然后一旦进入if,那就结束了。
public static void main(String [] args)
{
final double TEMPERATURE = 102.5;
double thermostat;
Scanner keyboard = new Scanner(System.in);
while (true) {
System.out.println("Enter the substance's temperature: ");
thermostat = keyboard.nextDouble();
if(thermostat > TEMPERATURE)
{
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
}
else if(thermostat < TEMPERATURE)
{
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
}
else if(thermostat == TEMPERATURE) {
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
}
else{
System.out.println("Enter the correct temperature value ");
}
}
}
小心,暂时不会停止,但你可以随意改变状态。
答案 1 :(得分:4)
如果你想一次又一次地获得温度,你必须使用循环。
myJSFunction()
答案 2 :(得分:1)
您的代码不是为了“重复”任何AKA迭代任务而构建的。在代码中添加一个循环,使其根据需要重复操作。
这里我们使用while (1==1)
创建一个无限循环。此外,我们在检查输入==“X”时添加退出条件,从循环中触发System.exit
。这有助于保持从应用程序的优雅退出,而不必强制关闭。
final double TEMPERATURE = 102.5;
double thermostat;
while(true){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the substance's temperature or press X to exit: ");
String input = keyboard.next();
if(input.equals("X")){
System.exit(0);
} else{
thermostat = Double.parseDouble(input);
if(thermostat > TEMPERATURE) {
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
} else if(thermostat < TEMPERATURE) {
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
} else if(thermostat == TEMPERATURE) {
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
} else {
System.out.println("Enter the correct temperature value ");
}
}
}
答案 3 :(得分:0)
Murtaza,你可以使用一个循环或者可以使用任何递归方法并在你的main方法中调用它。你所说的关于双倍不能被引用的错误是由于没有像equals这样的方法()在Scanner class.besides中,你不应该将int值与double类型变量进行比较。再次,要在if-else块中比较它,你应该使用Double中的equals()方法,包装类反对传递一个对象作为参数。