我在循环 if else 语句时遇到了麻烦,你能帮我一下吗?我想要发生的是,当数字更改为“热”和“冷”时,我希望它返回到 if else 语句的开头,以便能够再次更改数字。
import java.util.Scanner;
public class test {
public static void main(String args[])
{
Scanner gus = new Scanner (System.in);
double incrd, hot, cold;
hot = 10;
cold = 9;
System.out.println("press 1 for hotter");
System.out.println("press 2 for colder");
System.out.println("press 3 for more preasure");
System.out.println("press 4 for less pressure");
incrd = gus.nextDouble();
if (incrd == 1) {
hot += 2.5;
cold = cold - 2.5;
} else if (incrd == 2) {
hot = hot - 2.5;
cold += 2.5;
} else if (incrd == 3){
hot += 2.5;
cold += 2.5;
} else if (incrd == 4){
hot = hot - 2.5;
cold = cold - 2.5;
} else {
System.out.println ("invalid Number please enter either 1 or 2");
}
System.out.println(hot);
System.out.println(cold);
}
}
最佳答案
这将通过无限循环来完成。无限循环如下所示:
while (true) {
// do stuff
}
将要反复循环的代码放入循环中。你已经完成了。
嗯,快完成了。该代码永远不会停止。您将添加另一个菜单选项来停止程序,如果用户按下该数字,您将运行这行代码:
break;
这将停止循环和程序。打印出hot
和cold
。
关于java - 如何循环 if else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22976998/