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 ループを終了できるようにしましたが、このループを終了するより良い方法はありますか?