如何退出内部循环并返回到java中的主循环?

时间:2014-02-18 01:46:05

标签: java loops nested-loops

当我运行此代码时,这是一个包含许多不同选项的菜单。它由许多循环组成。其中一些我尚未制作。但是当我让用户选择“t”或硬币投掷模拟器时,我的问题就出现了。循环开始但是一旦用户输入硬币翻转量4,它说2.0头和2.0尾巴意味着50.0%是头 输入您选择的代码字母:COIN TOSS SIMULATOR 输入0退出。多少次投掷?

不应该说出您选择的字母:COIN TOSS SIMULATOR,输入0退出。有多少投掷?

当我输入0时,它表示您输入的选项无效。 't'不是有效选项。我想带回主菜单!!!!发生了什么????

public class toolBox {

    public static void main(String[] args) {

        Scanner myScanner = new Scanner(System.in);

        boolean properInput = false;

        int usersInput;

        while (!properInput) {

            System.out.println("Enter seed value:");
            if (myScanner.hasNextInt()) {
                usersInput = myScanner.nextInt();
                properInput = true;
                Random randomSeed = new Random(usersInput);

                String randomNumberList = "";

                for (int i = 0; i < 10; i++) {
                    randomNumberList += randomSeed.nextInt(80) + " ";
                }
            } else

            {
                String helloWorld = myScanner.next();
                System.out.println("You have not entered an integer. '" + helloWorld + "' is not an integer");

            }
        }

        outer: 
        System.out.println("===== CS302 TOOL BOX =====\nT > COIN TOSS SIMULATOR\nG > GRADE ESTIMATOR\nC > COLOR CHALLENGE\nQ > QUIT");

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

            boolean usersSelection = false;

            String c;
            outer: 
            while (!usersSelection) {
                {
                    System.out.print("" + "Type code letter for your choice: ");
                }
                if (anotherScanner.hasNext("q|Q")) {
                    c = anotherScanner.next();
                    usersSelection = true;

                    System.out.println("" + "" + "Good-Bye");
                    break;
                }

                if (anotherScanner.hasNext("t|T")) {
                    {
                        System.out.println("" + "COIN TOSS SIMULATOR" + "");
                    }
                    System.out.println("Enter 0 to quit. How many tosses?");
                    Random rand = new Random();
                    boolean headsOrTails;
                    float headsCount = 0;
                    float tailsCount = 0;
                    Scanner scanMan = new Scanner(System.in);
                    int numero = scanMan.nextInt();
                    if (numero == 0) {
                        break outer;
                    }

                    for (int j = 0; j < numero; j++) {

                        headsOrTails = rand.nextBoolean();
                        if (headsOrTails == true) {
                            headsCount++;
                        } else {
                            tailsCount++;
                        }
                    }
                    System.out.println(headsCount + " heads and " + tailsCount + " tails means "
                            + (headsCount / (headsCount + tailsCount) * 100 + "% were heads"));

                }
            }

            if (anotherScanner.hasNext("g|G")) // if the user were to enter either case of g, the
                                               // program will register both and initialize the
                                               // grade estimator.
            {
                c = anotherScanner.next();
                usersSelection = true;
            }

            if (anotherScanner.hasNext("c|C"))

            {
                c = anotherScanner.next();
                usersSelection = true;

                System.out.println("Welcome to the Color Challenge!");
            }

            else {
                String zoom = anotherScanner.next();
                System.out.println("You have entered an invalid option. '" + zoom + "' is not a valid option.");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你的问题不明确,但你的标题告诉我你认为有一个内在和外在的循环。

您没有内循环和外循环。

你的缩进非常混乱,但当我清理它然后删除了许多额外的代码行时,代码的结构变得清晰。

请注意以下事项:

1)你有两个循环,一个在顶部开启!properInput,另一个开启!usersSelection。还有一个for循环,但它没有做任何与你要问的代码流有关的事情。

2)您有两个相同的标签,一个在匿名代码块之外(请参阅下面代码中的我的评论),另一个在匿名块内。在这种情况下,它不会影响您的问题,但肯定是一个问题。

我的猜测是你的break outer行无效,因为你正在打破较低的while循环。

我建议您尝试将代码分段为函数,以使结构更清晰。

        while (!properInput) {

        }

        outer: 
        System.out.println("===== CS302 TOOL BOX =====\nT > COIN TOSS SIMULATOR\nG > GRADE ESTIMATOR\nC > COLOR CHALLENGE\nQ > QUIT");

        { /* begin anonymous code block */
            outer: 

            while (!usersSelection) {
                if (anotherScanner.hasNext("q|Q")) {
                    System.out.println("" + "" + "Good-Bye");
                    break;
                }

                if (anotherScanner.hasNext("t|T")) {
                    System.out.println("Enter 0 to quit. How many tosses?");
                    if (numero == 0) {
                        break outer;
                    }

                    for (int j = 0; j < numero; j++) {

                    }
                }
            }

        }