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");
}
}
}
}
}
This program creates a simple addition problem and asks the user to enter the answer. It also tracks the time it takes to answer the question correctly. I've tried to allow the user to end the first while loop by entering 100 for i, but is there a better way to end this loop?
Ctrl+Shift+F, NeatBeansAlt+Shift+FScanner sc = new Scanner(System.in);before loops.