我正在尝试做一个测验,如果答案正确,数字就会增加,如果答案不正确,则每个问题将有 4 次尝试
2 x 1 = ?
如果您回答正确,您必须转到2 x 2 = ?
以此类推,直到达到 10 分。如果您回答正确,每个问题将获得 5 分。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int points = 0;
int multiply;
System.out.println("please enter a number");
int yourNumber = input.nextInt();
for (multiply = 0; multiply<= 10; multiply++){
int yourAnswer = yourNumber * multiply;
System.out.println(yourNumber + " x " + multiply + " = ? ");
int theAnswer = input.nextInt();
for (int tries = 0; tries >= 4; tries++){
if (theAnswer == yourAnswer){
System.out.println("nice");
tries++;
最佳答案
你的代码似乎不完整,for循环部分应该是<=,因为你想在treis小于或等于4时保持循环 在您的情况下 >= 无法进入循环,因为 attempts = 0 并且 attempts 不大于 4 我还编辑为 attempts = 0, <=5,因为如果您想要 5 次尝试而不是数字,您可以直接更改
for (int tries = 1; tries <= 5; tries++){
if (theAnswer == yourAnswer){
System.out.println("nice");
points = points + 5;
break;
}
else{
System.out.println("Your answer : " + theAnswer + " is wrong, please try again. Attempts : " + tries);
theAnswer = input.nextInt();
}
}
关于java - 当达到最大尝试次数时如何停止以及如果答案错误则留在同一问题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58618563/