我試圖在5個數學題之後打破循環。 Return語句在此公共靜態中是必需的。 我似乎無法讓程序打破循環。5個問題後需要跳出一個while循環
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;
}
你有'break;'在最後,所以你打破循環。這是在調試器中單步執行代碼的地方,它會告訴你問題出在哪裏。 –
@PeterLawrey我對此很新,並且不知道調試器。 –
代碼中的中斷是我可以發現每次都會詢問不同問題的唯一方式。如果沒有休息,它會提出相同的問題五次,然後提出另一個問題5次 –