结束while循环的问题

时间:2015-02-25 02:49:32

标签: java while-loop

我试图让程序在总数达到10时结束,但由于某种原因,我的while循环在它达到10时继续计数。我有百分之百的百分比来找到10个问题的答案。

import java.util.*;

类CAI {     私有静态扫描仪输入;

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);
}

}

2 个答案:

答案 0 :(得分:1)

宣布你的while循环: “虽然总数小于或等于10”

这意味着它将在10点再次运行。

只要总计<10

编辑:你也不会在任何地方增加总数。

total++;

会做到的。

编辑:看来你做了。对不起代码很难在手机上阅读。

答案 1 :(得分:0)

这种情况可能有多种原因。其中一个原因是,如果变量难度不等于1(这个变量在你的代码中也找不到),则总数将永远不会增加,因此第一个while循环将永远持续。因为您还没有共享所有代码,所以ans != (num[0] * num[1]永远不会是真的,因此,最内层的do / while循环将永久执行。如果您发布其余代码,我们将能够为您提供更多帮助 编辑:好的,谢谢。看起来你的问题是因为当用户继续得到错误的问题时,他们无法逃脱内部while循环,因此无法逃脱外部while循环。对此的解决方案很简单,只需将while (ans != (num[4] * num[5]));更改为ans != (num[0] * num[1]) && total < 10,从而在用户提出错误的问题时为代码提供一种逃避方法。