Blankets_McGee Blankets_McGee - 2 months ago 20
Java Question

Replace 'menu' with input from user

In the interest of learning I decided to write up a coin flipping program. The coin is an enum and i have the program return that enum value. I also have the user input from a menu style but this was helped by following along in a Barnes and Nobles book I purchased a while back.

I think I have come to a weird cross road. i was wanting to basically return the enum value and such but remove the 'menu' aspect and replace it with the ability for the user to input how many flips they would like to do and also repeat the program if they want to (so instead of pressing 1 to flip each time they can input say 20000 and it would flip that many times i also think doing this would help with a fairness check as a true test of fairness would return almost even amounts of heads and tails if it were to flip that many times then pressing 0 for no flips would end the program) and i want to prompt the user and ask if they would like to repeat.

here is the program I have written:

import java.util.*;

public class CoinTossing
{
private enum Coin { HEADS, TAILS };

private static final Random randomNumbers = new Random();

private static final int HEAD = 1;
private static final int TAIL = 2;

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

int choice;
int toss = 0;
int tosses = 0;
int frontflip = 0;
int backflip = 0;

Coin gameStatus;

System.out.println("Welcome to the Coin Toss Program.\n");
System.out.println("Choose from the menu what you want to do.");
System.out.print("1. Toss the coin\n2. Quit program\n");
choice = input.nextInt();

while ( choice != 0 )
{
if ( choice == 1 )
{
int CoinTossed = Flip();

switch ( CoinTossed )
{
//added tosses to switch statement to make the counter work perfect.
case HEAD:
gameStatus = Coin.HEADS;
tosses++; // add amount of tosses
break;
default: // changed case TAIL to default. Easy and works.
gameStatus = Coin.TAILS;
tosses++; // add amount of tosses
break;
}

if ( gameStatus == Coin.HEADS )
{
frontflip++; //Add amount of heads
}
else // gameStatus == TAILS
backflip++; //Add amount of tails
}

// A try to make an real exit out of a program

if ( choice == 2 )
{
EndProgram( frontflip, backflip, tosses );
}

System.out.println("\nChoose from the menu what you want to do.");
System.out.print("1. Toss the coin\n2. Quit program\n");
choice = input.nextInt();
}
}

//Toss the coin to determine 1 or 2.
public static int Flip()
{
int toss;

toss = 1 + randomNumbers.nextInt( 2 );

if ( toss == 1 )
{
System.out.println("You toss the coin and it lands on head!");
}
else
{
System.out.println("You toss the coin and it lands on tail!");
}
return toss;
}

public static void EndProgram( int frontflip, int backflip, int tosses )
{
System.out.printf("You have tossed %d times.\n", tosses);
System.out.printf("Of all those tosses, %d landed on heads, and %d on tails.\n", frontflip, backflip);
System.exit(0);
}
}


I think I need a
do
/
while
loop so that I can have the user answer the yes or no question of do you want to play again? and inside the loop I have a switch statement that also says if the user inputs 0 for the number of flips the program ends?

I thought I could add this snippet to get input:

System.out.println("How many flips do you want?");
System.out.println("(0 will exit the program)");
number = input.nextInt();


I was thinking of creating a new variable and have the user set the number of tosses. Then compound the while loop check like so

while(choice != 0 && numTosses !=0)


and then decrease the count and I'll have to check that count and once it reaches 0 print results as far as how many heads and how many tails then prompt the user if they would like to play the game again but I am having trouble getting the right. honestly I don't even know why I'm trying to do this if but for the knowledge aspect so if you don't wanna help a broski out I understand. I feel like I am on the right track.

Answer

You can use 2 loops:

public class CoinFlip {

    private enum Coin {
        HEADS,
        TAILS
    };

    public static void main(String[] arguments) {

        new CoinFlip();
    }

    CoinFlip() {

        Scanner input = new Scanner(System.in);
        int heads = 0, tails = 0;

        while (true) {
            System.out.println("How many flips do you want?");
            System.out.println("(0 will exit the program)");
            int number = input.nextInt();
            if (number == 0)
                break; // or System.exit

            for (int i = 0; i < number; i++) {
                Coin flipResult = flip();
                switch (flipResult) {
                    case HEADS:
                        heads++;
                        break;
                    case TAILS:
                        tails++;
                        break;
                }
            }
            System.out.println("Heads: " + heads);
            System.out.println("Tails: " + tails);
        }
    }

    private Coin flip() {

        return Coin.values()[(int) (Math.random() * Coin.values().length)];
    }
}

The while loop continues to ask the user to play again and again, if they put 0 your break it or exit it. In that loop, a for loop will iterate over the flips.