【问题标题】:Issue with ending a while loop结束while循环的问题
【发布时间】:2015-02-25 02:49:31
【问题描述】:

我试图在总数达到 10 时结束程序,但由于某种原因,我的 while 循环在达到 10 时继续计数。一旦回答了 10 个问题,我就有 int percent 来找到百分比。

import java.util.*;

类蔡{ 私有静态扫描器输入;

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

}

【问题讨论】:

    标签: java while-loop


    【解决方案1】:

    您的 while 循环已声明: "而总数小于或等于十"

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

    总之

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

    total++;
    

    会做的。

    编辑:看来你这样做了。抱歉,代码很难在手机上阅读。

    【讨论】:

    • 他确实在第二个 do-while-loop 开始时增加总数。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 1970-01-01
      • 2014-04-20
      • 2014-10-31
      • 2019-03-15
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多