Java 如何让我的程序返回主菜单?

Java 如何让我的程序返回主菜单?,java,Java,在第二次while循环中,当用户选择掷硬币模拟器选项时,我遇到问题。当用户选择0时,程序不会像我希望的那样返回主菜单,而是停止,并且不会循环返回主菜单供用户选择另一个选项 有没有办法解决这个问题?我不能使用多种方法,因为这是我正在做的项目的一个要求。我已经在这一部分上呆了很长时间了,一个星期了,如果你能给我指点方向,我将不胜感激 下面是当用户在运行抛硬币模拟器后选择零时,我的程序的样子 ====CS302工具箱===== 掷硬币模拟器 G>等级估计器 颜色挑战赛 Q>退出 键入代码字母供您选择:

在第二次while循环中,当用户选择掷硬币模拟器选项时,我遇到问题。当用户选择0时,程序不会像我希望的那样返回主菜单,而是停止,并且不会循环返回主菜单供用户选择另一个选项

有没有办法解决这个问题?我不能使用多种方法,因为这是我正在做的项目的一个要求。我已经在这一部分上呆了很长时间了,一个星期了,如果你能给我指点方向,我将不胜感激

下面是当用户在运行抛硬币模拟器后选择零时,我的程序的样子

====CS302工具箱=====

掷硬币模拟器

G>等级估计器

颜色挑战赛

Q>退出

键入代码字母供您选择:t

掷硬币模拟器

输入0退出。有多少次投掷

四,

2.0头和2.0尾意味着50.0%是头

输入0退出。有多少次投掷

三,

3.0头和0.0尾表示100.0%为头

输入0退出。有多少次投掷

0

然后它只是停止,而不是重定向回主菜单

  {        
           Scanner anotherScanner = new Scanner(System.in);

            boolean usersSelection = false;

            outer:
                while (!usersSelection)
                { 
                    System.out.println("===== CS302 TOOL BOX =====\nT > COIN TOSS SIMULATOR\nG > GRADE ESTIMATOR\nC > COLOR CHALLENGE\nQ > QUIT");
                    String c;
                    System.out.print(""+ "Type code letter for your choice: ");

                    if (anotherScanner.hasNext("q|Q"))
                    {

                        c = anotherScanner.next();
                        //usersSelection = true;
                        System.out.println("\n Good-bye");
                        break;
                    }
                    else if (anotherScanner.hasNext("t|T")){

                        c = anotherScanner.next();
                        usersSelection = true;
                        System.out.println("");
                        System.out.println("COIN TOSS SIMULATOR");
                        System.out.println("");
                        System.out.println("Enter 0 to quit. How many tosses?");

                        Random rand = new Random();

                        Scanner insideScanner = new Scanner(System.in);
                        int feeble = insideScanner.nextInt();

                        double heads = 0;
                        double tails = 0;
                        boolean hvt;



                        while ( feeble != 0 ) { //Pay attention to this while loop

                            if (feeble == 0){break outer;}

                            for (int i =0; i < feeble; i++) {
                                hvt = rand.nextBoolean();

                                if (hvt == true){ heads++;}

                                else {tails++;}

                                }


                        System.out.println(heads + " heads and " + tails + " tails means " + (heads/(heads+tails)*100 + "% were heads"));
                        System.out.println("Enter 0 to quit. How many tosses?"); //I ask the question again
                        heads = 0;
                        tails = 0;
                        feeble = insideScanner.nextInt();//I get new input

                        }
                    }
usersSelection仍然为true,因此外部循环为!用户选择不再运行


同样,如果弱==0{break outer;}是冗余的,即使它确实工作了,也要break outer;将通过退出外部循环来结束程序。

如果弱==0{break outer;}这怎么可能是真的?您的while循环仅在以下条件下执行:虚弱!=0,在if语句之前,我看不到您更改了衰弱的值。因此,非常确定该条件始终为false。我尝试将其放置在while循环之外,但它没有重定向。然后我会说if diffle==0usersSelection=false?if diffle==0{break outer;}的代码永远不会运行。删除它,这样你就不会混淆自己。原因是,如果wevele为0,则while循环whilefeeble!=0将不会运行,因此甚至无法到达if语句。