编程新手。
我有一个java类,我正在尝试编写这个程序,但它不停地打印,甚至还干扰我的浏览器! (我使用在线IDE)
我的指示: 问:你有多爱我? 10以下:错误答案 等于 10 及以上 = 好答案
我知道该怎么做,但我不希望每次我写答案时程序都停止。我希望程序继续运行,直到我得到 10+。所以我总是可以输入,直到 10 分中的 10+ 为止。
这是我的台词:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println ("how much do you love me out of 10");
int num1 = input.nextInt();
while (true)
if (num1 <= 9) {
System.out.println("Wrong answer");
}
else {
System.out.println ("Good answer");
}
}
}
最佳答案
这应该有效:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println ("Out of 10, how much do you love me?");
int input = 0
while (input <= 9) {
input = scanner.nextInt();
if (input <= 9) {
System.out.println("Wrong answer");
} else {
System.out.println("Good answer");
}
}
}
}
解释:它会循环直到输入不小于或等于 9 ( while (input <= 9)
)。每次循环时,它都会获取输入并对其进行操作。
希望这有帮助!
关于java - 尝试使用循环时无限打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461348/