import java.io.*;
import java.util.Scanner;
public class Solve {
public static void main(String[] args) {
int max = 10;
int i = 0;
long record = 100000;
while (i != 100) {
int x = (int) (Math.random() * max) + 1;
int y = (int) (Math.random() * max) + 1;
int solution = x + y;
long startTime = System.currentTimeMillis();
while (i != solution) {
System.out.println("Enter the solution to " + x + "+" + y);
Scanner sc = new Scanner(System.in);
i = sc.nextInt();
if (i != solution) {
System.out.println("Incorrect");
} else {
System.out.println("Correct!");
}
long endTime = System.currentTimeMillis();
System.out.println("You took: " + (endTime - startTime) + "ms");
if (endTime - startTime < record) {
record = endTime - startTime;
System.out.println("New record time: " + record + "ms");
}
}
}
}
}
该程序创建一个简单的加法问题并要求用户输入答案。它还跟踪正确回答问题所需的时间。我试图让用户通过输入 100 for i 来结束第一个 while 循环,但是有没有更好的方法来结束这个循环?