【发布时间】:2017-11-28 23:28:33
【问题描述】:
我是一个初学者编程,我想用数组问多个问题,并告诉用户他每个问题是对还是错,我设法让它运行,但现在我如何实现代码以便用户最多只能尝试 3 次才能正确回答问题。
for(int n = 0; n <QArray.length; n++)
{
System.out.println("Question" + (n+1));
System.out.println(QArray[n]);
String ans = scanner.nextLine();
if (ans.equalsIgnoreCase(AArray[n]))
{
System.out.println("That is correct!");
}
else
{
System.out.println("That is incorrect!");
}
}
【问题讨论】:
-
您可以使用一个
for循环,最多计数为 3。 -
while (attempts > 0)然后每次递减attempts?
标签: java arrays loops if-statement