Java 5个问题后需要打破while循环

Java 5个问题后需要打破while循环,java,loops,count,Java,Loops,Count,5道数学题后,我试图打破循环。 此公共静态文件中需要Return语句。 我似乎无法让程序打破循环 count=0; while(count < 5) { System.out.println("Please answer the following problem: "); System.out.println(randomInt1 + "+" + randomInt2 + "="); answer = keyboard.nextDouble();

5道数学题后,我试图打破循环。 此公共静态文件中需要Return语句。 我似乎无法让程序打破循环

count=0;
while(count < 5)
{
    System.out.println("Please answer the following problem: ");
    System.out.println(randomInt1 + "+" + randomInt2 + "=");
    answer = keyboard.nextDouble();   

    if(answer != (randomInt1 + randomInt2))
    {
        System.out.println("Sorry, that is not correct.");
    }     
    else if(answer == (randomInt1 + randomInt2))
    {
        System.out.println("Nice!");
    }
    count++;
    break;
}
count=0;
同时(计数<5)
{
System.out.println(“请回答以下问题:”);
System.out.println(randomInt1+“+”+randomInt2+“=”);
答案=键盘.nextDouble();
如果(回答!=(随机点1+随机点2))
{
System.out.println(“对不起,这不正确。”);
}     
else if(答案==(randomInt1+randomInt2))
{
System.out.println(“很好!”);
}
计数++;
打破
}

断点
移动到
内,否则
。当您得到正确答案或
count
大于或等于5时,循环将停止

目前,您的循环只执行一次,然后您就中断了它


另外,如果你想问一个不同的问题,那么每次循环都需要新的随机数

不要使用break,当条件失败时它会自动停止。删除else if并使其成为just else如果if case中的值不相等,则表示在else case中它应该相等。如果需要在用户输入正确的值时停止,只需将break语句移到if语句中,如果他回答不正确,那么它会一次又一次地问同样的问题,直到条件失败

count=0;

while(count < 5)
{
 System.out.println("Please answer the following problem: ");
System.out.println(randomInt1 + "+" + randomInt2 + "=");
answer = keyboard.nextDouble();   

 if(answer == (randomInt1 + randomInt2))
{
 System.out.println("Nice!");
 break;
}     
 else 
{
 System.out.println("Sorry, that is not correct.");
}

 count++;

}
count=0;
同时(计数<5)
{
System.out.println(“请回答以下问题:”);
System.out.println(randomInt1+“+”+randomInt2+“=”);
答案=键盘.nextDouble();
如果(答案==(随机点1+随机点2))
{
System.out.println(“很好!”);
打破
}     
其他的
{
System.out.println(“对不起,这不正确。”);
}
计数++;
}

没有“中断”,它一遍又一遍地问同一个数学问题5次,而另一个数学问题又一遍又一遍地问5次。我想我明白你的意思了。看editsHow我要检查一下吗?重读答案。也许可以刷新页面。我改变了我原来的回答,告诉你移动断点而不是扔掉它你有
break在最后,这样你就打破了循环。这就是在调试器中单步执行代码将向您显示问题所在的地方。@PeterLawrey我对这一点非常陌生,不知道有调试器。代码中断是我能找到的唯一方法,每次都会问不同的问题。如果你想重复5个问题,你需要把这些问题放到循环中。如果你想问一个新问题,你需要一个新的随机数。这个编译了吗?您有语法错误,请检查它。它在else-if语句附近。我这边没有任何错误,不,不,它会工作得很好