0

When I run this code, which is a menu with many different options. it consists of many loops. Some of which I have yet to make. But my issue arises when I have the user select "t" or the coin toss simulator. The loop begins but once the user enters the amount of coin flips say 4, it says 2.0 heads and 2.0 tails means 50.0% were heads Type code letter for your choice: COIN TOSS SIMULATOR Enter 0 to quit. How many tosses?

It shouldn't say type the letter for your choice: COIN TOSS SIMULATOR, enter 0 to quit. how many tosses?

Also when I enter 0 it says You have entered an invalid option. 't' is not a valid option. I want to Bring back the main menu!!!! what is going on????

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.");
            }
        }
    }
}
6
  • Don't get confused. Eclipse, a text editor, has no effect on this. Commented Feb 18, 2014 at 1:47
  • To start with, I would suggest breaking your main() into multiple functions. that should help your organize your code and see what is happening. Second, I would suggest avoiding using labels, as you will almost definitely have unexpected behavior. Commented Feb 18, 2014 at 1:59
  • Furthermore, I would suggest that while I have seen "break;" used in code, I have written an awful lot of code and I have never required the use of "break". Philosophically, "break" disrupts the logical flow of the loop, and a breaking condition would be better managed by setting other conditions in a while or repeat...until loop. Commented Feb 18, 2014 at 2:23
  • @david -- From a comp sci point of view I agree with you, but from a software engineering (and maintainability!) angle, I have to strongly disagree.There are cases where the conditionals necessary to avoid break really are a much less readable alternative. As with many such rules of thumb, it's important to know when to avoid them. Think of it as a local exception, but much cheaper. Commented Feb 18, 2014 at 4:36
  • @JBCP: Labelled break is a rarely used construction in Java, but sometimes a valuable one. I'd rather that they hadn't expressed it as labels (some languages handle it by specifying which loop variable you're breaking to), but not all labels are evil -- and Java labels are rare enough that I'm willing to forgive them this one use. Commented Feb 18, 2014 at 4:40

1 Answer 1

1

Your question is not clear, but your title suggests to me you think there is an inner and outer loop.

You don't have an inner and an outer loop.

Your indentation was really messy, but when I cleaned it up and then deleted a lot of extra lines of code, the structure of the code became clear.

Notice the following:

1) You have two loops, one on top switched on !properInput, the lower one switched on !usersSelection. There is also a for loop, but it doesn't do anything related to the code flow you are asking about.

2) You have two identical labels, one outside an anonymous block of code (see my comment in the code below), and another inside the anonymous block. In this case it doesn't affect your question, but it is definitely a problem.

My guess is that your break outer line isn't working because you are breaking out of the lower while loop.

I suggest you try fragmenting your code into functions to make the structure clearer.

        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++) {

                    }
                }
            }

        }
Sign up to request clarification or add additional context in comments.

2 Comments

I am legitimately surprised that the original code compiles with duplicate labels, but I'll be darned, it does.
I'm surprised anyone still uses labels. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.