jack
jack

Reputation: 3

Issue with ending a while loop

I am trying to make the program end when total reaches 10, but for some reason my while loop continues counting when it reaches 10. I have the int percent to find the percent once 10 questions are answered.

import java.util.*;

class CAI { private static Scanner input;

public static void main(String[] arguments) {
    menu();// calls menu method
    compute();// calls compute method
}

public static void menu() {// method that displays menu
    System.out.println(" CAI MENU ");
    System.out.println("\n)");
    System.out
            .println("\n1) DIFFICULTY 1\n2) DIFFICULTY 2\n3) DIFFICULTY 3\n4) DIFFICULTY 4");

}

public static int[] Blop() {
    Random rand = new Random();
    int arr[] = new int[8];
    arr[0] = rand.nextInt(9);
    arr[1] = rand.nextInt(9);
    arr[2] = rand.nextInt(99);
    arr[3] = rand.nextInt(99);
    arr[4] = rand.nextInt(999);
    arr[5] = rand.nextInt(999);
    arr[6] = rand.nextInt(9999);
    arr[7] = rand.nextInt(9999);
    return arr;
}

public static void compute() {
    int difficulty;
    input = new Scanner(System.in);
    System.out.println("Enter an option: ");
    difficulty = input.nextInt();
    int total = 0;
    int percent = 0;
    while (total <= 10) {
        if (difficulty == 1) {
            int num[] = new int[2];
            int ans;
            String choice;
            do {
                num = Blop();
                do {
                    System.out.print("How much is " + num[0] + " times "
                            + num[1] + " ? :");
                    total++;
                    System.out.print(total);
                    ans = input.nextInt();
                    String Correct;
                    String Wrong;
                    String[] correct = { "Very good! ", "Excellent! ",
                            "Nice work! ", "Keep up the good work! " };
                    String[] wrong = { "No. Please try again. ",
                            "Wrong. Try once more. ", "Don’t give up! ",
                            "No. Keep trying " };
                    Random rand = new Random();
                    Correct = correct[rand.nextInt(correct.length)];
                    Wrong = wrong[rand.nextInt(wrong.length)];
                    if (ans == (num[0] * num[1])) {
                        System.out.print(Correct);
                        percent++;
                    } else {
                        System.out.print(Wrong);
                    }

                } while (ans != (num[0] * num[1]));
                System.out.print("Do you want more questions(yes/no) :");
                input.nextLine();
                choice = input.nextLine();
            } while (choice.equalsIgnoreCase("yes"));
        }
    }

    if (difficulty == 2) {
        int num[] = new int[2];
        int ans;
        String choice;
        do {
            num = Blop();
            do {
                System.out.print("How much is " + num[2] + " times "
                        + num[3] + " ? :");
                ans = input.nextInt();
                String Correct;
                String Wrong;
                String[] correct = { "Very good! ", "Excellent! ",
                        "Nice work! ", "Keep up the good work! " };
                String[] wrong = { "No. Please try again. ",
                        "Wrong. Try once more. ", "Don’t give up! ",
                        "No. Keep trying " };
                Random rand = new Random();
                Correct = correct[rand.nextInt(correct.length)];
                Wrong = wrong[rand.nextInt(wrong.length)];
                if (ans == (num[2] * num[3])) {
                    System.out.print(Correct);
                } else {
                    System.out.print(Wrong);
                }
            } while (ans != (num[2] * num[3]));
            System.out.print("Do you want more questions(yes/no) :");
            input.nextLine();
            choice = input.nextLine();
        } while (choice.equalsIgnoreCase("yes"));
    }
    if (difficulty == 3) {
        int num[] = new int[2];
        int ans;
        String choice;
        do {
            num = Blop();
            do {
                System.out.print("How much is " + num[4] + " times "
                        + num[5] + " ? :");
                ans = input.nextInt();
                String Correct;
                String Wrong;
                String[] correct = { "Very good! ", "Excellent! ",
                        "Nice work! ", "Keep up the good work! " };
                String[] wrong = { "No. Please try again. ",
                        "Wrong. Try once more. ", "Don’t give up! ",
                        "No. Keep trying " };
                Random rand = new Random();
                Correct = correct[rand.nextInt(correct.length)];
                Wrong = wrong[rand.nextInt(wrong.length)];
                if (ans == (num[4] * num[5])) {
                    System.out.print(Correct);
                } else {
                    System.out.print(Wrong);
                }
            } while (ans != (num[4] * num[5]));
            System.out.print("Do you want more questions(yes/no) :");
            input.nextLine();
            choice = input.nextLine();
        } while (choice.equalsIgnoreCase("yes"));
    }
    if (difficulty == 4) {
        int num[] = new int[2];
        int ans;
        String choice;
        do {
            num = Blop();
            do {
                System.out.print("How much is " + num[6] + " times "
                        + num[7] + " ? :");
                ans = input.nextInt();
                String Correct;
                String Wrong;
                String[] correct = { "Very good! ", "Excellent! ",
                        "Nice work! ", "Keep up the good work! " };
                String[] wrong = { "No. Please try again. ",
                        "Wrong. Try once more. ", "Don’t give up! ",
                        "No. Keep trying " };
                Random rand = new Random();
                Correct = correct[rand.nextInt(correct.length)];
                Wrong = wrong[rand.nextInt(wrong.length)];
                if (ans == (num[6] * num[7])) {
                    System.out.print(Correct);
                } else {
                    System.out.print(Wrong);
                }
            } while (ans != (num[6] * num[7]));
            System.out.print("Do you want more questions(yes/no) :");
            input.nextLine();
            choice = input.nextLine();
        } while (choice.equalsIgnoreCase("yes"));
    }
    System.out.print(100 / 10 * percent);
}

}

Upvotes: 0

Views: 54

Answers (2)

user2577576
user2577576

Reputation:

There could be multiple reasons for this happening. One such reason is that if the variable difficulty does not equal one(this variable is nowhere to be found in your code also), then total will never be incremented, and as a result the first while loop will last forever. Because you have not shared all of your code, it could also be that ans != (num[0] * num[1] is never true, and as a result, the innermost do/while loop is executed forever. If you post the rest of your code, we will be able to assist you more.
EDIT: OK, thanks. It looks like your problem is due to the fact that while the user continues to get the question wrong, they cannot escape the inner while loop, therefore not being able to escape the outer while loop. The solution to that is easy, simply changing while (ans != (num[4] * num[5])); to ans != (num[0] * num[1]) && total < 10, thereby giving the code a way to escape while the user is getting the question wrong.

Upvotes: 0

Justin Tokarchuk
Justin Tokarchuk

Reputation: 159

Your while loop is declared: "while total is less than or equal to ten"

which means it will run once again at 10.

just make it while total <10

edit: you also don't increment total anywhere.

total++;

will do it.

edit: it appears you do. sorry the code is hard to read on a phone.

Upvotes: 1

Related Questions