gpt4 book ai didi

java - 在哪里正确放置 while-do 语句?

转载 作者:行者123 更新时间:2023-12-01 09:30:28 25 4
gpt4 key购买 nike

我不久前刚刚开始编程,我真的很想了解更多。现在,我正在创建一个小的“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;
}
}
}
}
}
}

最佳答案

您将输入代码排除在循环之外:

int number = FirstScan.nextInt();
int chopperCrash = number;

while (true) {

由于您从未在循环内分配chopperCrash,因此它会永远持续下去。所以,像这样把它放进去:

while (true) {
int number = FirstScan.nextInt();
int chopperCrash = number;

关于java - 在哪里正确放置 while-do 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39473981/

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