0

I'm just starting Java and this is a coin flip program that I've written recently. So it's supposed to produce sequences of coin flips that meet the requirements that the user sets, however when it gets to the end it should ask if the user wants to go again. I'm having an issue where it will print the question twice when it gets down to end. I really need to figuer this out so any suggestions/clarifications for my code would be greatly appreciated.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    outer: while (true) {
        System.out.print("Ready to run a coin flip simulation. Enter the number of sequences: ");
        int sequences = scan.nextInt();

        System.out.print("How many heads should each sequence have? ");
        int heads = scan.nextInt();

        System.out.print("How many tails should each sequence have? ");
        int tails = scan.nextInt();

        System.out.println("Simulating Sequences");

        int tFlips = 0;
        int mFlips = 0;

        for (int i = 1; i <= sequences; i++) {
            int h = 0;
            int t = 0;
            String s = "";
            while (t < tails || h < heads) {
                if (Math.random() < 0.5) {
                    h++;
                    s += "H";
                } else {
                    t++;
                    s += "T";
                }
                tFlips++;
            }

            if (t + h > mFlips) {
                mFlips = t + h;
            }

            System.out.println(i + " - " + s);
            h = 0;
            t = 0;
            s = "";

        }
        System.out.printf("The average number of flips was " + ((float) tFlips / sequences) + " and maximum was %d", mFlips);

        System.out.println("\r\n");

        boolean go = true;

        while (go) {
            System.out.print("Would you like to run another simulation? (y/n): ");
            String c = scan.nextLine();

            if (c.equalsIgnoreCase("y")) {
                break;
            } else if (c.equalsIgnoreCase("n")) {
                break outer;
            } else {
                continue;
            }
        }
        System.out.print("\r\n");
    }
}

2 Answers2

1

Your String c = scan.nextLine(); reads a new line from your scan.nextInt() calls. You can read more here Scanner is skipping nextLine() after using next() or nextFoo()?

It is good practice to call .nextLine() after every call to .nextInt() to always consume the newline character.

sleepToken
  • 1,804
  • 1
  • 12
  • 21
Butiri Dan
  • 1,704
  • 5
  • 10
  • 18
0

Change the String c = scan.nextLine(); to String c = scan.next();. And you don't really need the while(go) loop, it's more simple if you're doing this:

System.out.print("Would you like to run another simulation? (y/n): ");
String c = scan.next();
if (!c.equalsIgnoreCase("y"))
   break;

P.S.: here is a good answer why the nextLine() isn't working correctly. (by @Rohit Jain)

user1234SI.
  • 1,561
  • 3
  • 17
  • 2
    I disagree. What OP should actually do is consume the newline character with `scan.nextLine()` after each call to `scan.nextInt()` – sleepToken Jan 26 '20 at 22:53
  • This is his call after my help. I totally agree with you :) The answer provided by @Rohit Jain tells him what you've said. – user1234SI. Jan 26 '20 at 22:54
  • Thank you for the scan fix, I had no idea why that was happening and it was very frustrating. As for the while loop suggestion, I can see how that would work but my assignment requires that I keep asking if they don't respond with a y or an n. I know it's not super efficient but it seems to be working as is. Thanks again for the help everyone! – dosorno2619 Jan 26 '20 at 23:15
  • Mhm yes, I've thought about this later, but still, no need of `while(go)` loop. You're welcome :) – user1234SI. Jan 26 '20 at 23:24