1
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?

3
  • 3
    Have you tried running it in a debugger(your IDE should offer one) to find where the iterative behavior goes wrong? Commented Jun 26, 2013 at 0:54
  • I formatted your code a little to make it more readable. In the future please add code in this form. You can easily get it in IDEs like Eclipse Ctrl+Shift+F, NeatBeans Alt+Shift+F Commented Jun 26, 2013 at 1:00
  • Not the solution, but move Scanner sc = new Scanner(System.in); before loops. Commented Jun 26, 2013 at 1:07

2 Answers 2

2

You never break out of the inner while loop. Your execution path will be stuck there. A quick fix is to adjust your looping constraint on the inner loop:

while (i != solution && i != 100)
Sign up to request clarification or add additional context in comments.

Comments

1

Unless 100 is also the solution, it will never exit the inner loop. Simplest fix for that is to break out of that loop when the user enters 100, such as with:

i = sc.nextInt();
if (i == 100) break; // add this line
if (i!=solution) ...

That way, you'll exit the inner loop which continuously checks for a correct answer, and the outer loop will then exit as well.

There may be better ways to indicate that the user has finished but this will suffice for this case, since the numbers will never sum to 100. If you ever intend to use higher numbers that could sum to 100, you may want to consider the use of -1 as the sentinel (if you ever get to the point where the result can be negative, you'll have to adjust for that as well).

You might also want to make it clear to the user that 100 is the sentinel value so that they're not guessing how to exit the program:

System.out.println ("Enter the solution to " + x + "+" + y + " or 100 to exit");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.