我刚开始编程不久前,我真的很想学习更多。现在,我正在创建一个小的“if-else”,“while-do”场景,用户将使用正确的整数来回答问题。我遇到了麻烦,每当我点击这段代码时都会遇到麻烦:
if (chopperCrash > 100) {
System.out.println("That's not possible!");
System.out.println("Hurry and re-enter a new value before we crash!");
continue;
它不断循环打印的文本。有人可以帮我解决这个具体的项目,并向我详细解释“if-else”和“while-do”语句的正确位置吗?感谢您的任何帮助。我很抱歉,如果这已经得到回答,我太过密集,无法理解其他先前提出的问题。这是我的完整代码块:
import java.util.Scanner;
public class FirstClass {
public static void main (String[] args){
System.out.println("The chopper is going down because your lack of experience.");
System.out.println("How much throttle are you going to give it?");
System.out.println("(enter a number between 1 and 100)");
Scanner FirstScan = new Scanner(System.in);
int number = FirstScan.nextInt();
int chopperCrash = number;
while (true){
if (chopperCrash >= 60 && chopperCrash <= 100){
System.out.println("Looks like we'll make it another day!");
break;
}
else{
if (chopperCrash > 100) {
System.out.println("That's not possible!");
System.out.println("Hurry and re-enter a new value before we crash!");
continue;
}
else{
if (chopperCrash <= 59) {
System.out.println("Not enough throttle!");
System.out.println("We're going down!");
break;
}
}
}
}
}
}
答案 0 :(得分:1)
您将输入代码留在循环中:
int number = FirstScan.nextInt();
int chopperCrash = number;
while (true) {
由于你从未在循环中分配chopperCrash
,因此它会永远持续下去。所以,把它放进去就好了:
while (true) {
int number = FirstScan.nextInt();
int chopperCrash = number;