1

So I have fixed the continuation issue using input.nextLine(); however, when I run the code, it will work through two iterations and then exit the program. I noticed that it does this when the code is completely repeated again. My code now looks like this:

package morrisJCh4Sec8to9;

import java.util.Scanner;

public class CoinToss {
    public static void main (String args[]) {
        Scanner input = new Scanner (System.in);
        String call = " ";
        String heads = "h";
        String tails = "t";
        String play = " ";
        String playAgain = "c";
        double numWins = 0;
        double totPlay = 0;
        if(call == null || call.length() > 1) {
            System.exit(1);
        }

        int toss = (int)(Math.random() * 2);
        do {
            System.out.println("Please call the coin toss (h or t): "); 
            //this line keeps being repeated 
            call = input.nextLine();
            System.out.println("Tossing...");
            //this line shows up before it is supposed to
            if(heads.equals(call)) { //checks if the call is equal to heads
                System.out.println("The coin came up heads. You win!");
                numWins = numWins + 1; //keeps track of the wins
                totPlay = totPlay + 1; //keeps track of total times played

            }
            else if(call.equals(tails)) { //if call is tails
                System.out.println("The coin came up heads. Sorry you 
                lose!");
                totPlay = totPlay + 1;
            }

            else if(tails.equals(call)) {
            //if call is tails
                System.out.println("The coin came up tails. You win!");
                numWins = numWins + 1;
                totPlay = totPlay + 1;
                }
            else {
                System.out.println("The coin came up tails. Sorry you 
                lose!"); // call was heads
                totPlay = totPlay + 1;
            }   


                System.out.println("Would you like to play again? (type c to 
                continue or any other letter to quit)");

                play = input.next();
                if(play.compareTo(playAgain) != 0) {
                    System.out.println("Thanks for playing!");
                    break;
                }
                else { //goes through the iteration again
                    System.out.println("Please call the coin toss (h or 
                    t)"); 
                    input.nextLine();

                    call = input.nextLine();
                    System.out.println("Tossing...");
                   if(heads.equals(call)) { //checks if the call is equal to heads
                System.out.println("The coin came up heads. You win!");
                numWins = numWins + 1; //keeps track of the wins
                totPlay = totPlay + 1; //keeps track of total times played

            }
            else if(call.equals(tails)) { //if call is tails
                System.out.println("The coin came up heads. Sorry you 
                lose!");
                totPlay = totPlay + 1;
            }

            else if(tails.equals(call)) {
            //if call is tails
                System.out.println("The coin came up tails. You win!");
                numWins = numWins + 1;
                totPlay = totPlay + 1;
                }
            else {
                System.out.println("The coin came up tails. Sorry you 
                lose!"); // call was heads
                totPlay = totPlay + 1;
            }   


                System.out.println("Would you like to play again? (type c to 
                continue or any other letter to quit)");

                play = input.next();
                if(play.compareTo(playAgain) != 0) {
                    System.out.println("Thanks for playing!");
                    break;
                }
                    continue; //after two times of iteration, the code exits
                }

        }
        while(toss < 1);

        System.out.println("Great game! You were correct " + (int) ((numWins 
        * 100 ) / totPlay) + "% of the time."); //prints the number of times 
        //user was correct.
        input.close();
    }
}

The output looks like this: Please call the coin toss (h or t): h Tossing... The coin came up heads. You win! Would you like to play again? (type c to continue or any other letter to quit) c Please call the coin toss (h or t): h Tossing... The coin came up heads. You win! Would you like to play again? (type c to continue or any other letter to quit) c Great game! You were correct 100% of the time.

  • 4
    This sounds like a *great* opportunity to start familiarizing yourself with the use of a debugger. Using a debugger you can step through the code line by line as it executes, observing the runtime values of your variables and runtime behavior of your logic. This will allow you to narrow down specifically where your logic *first* deviates from what you expect, what the values are when that happens, where those values came from, etc. When you do this, where specifically does this occur? What line of code produces an unexpected results? What result was expected? Why? – David Nov 06 '18 at 20:10

0 Answers0