简体   繁体   中英

Java Coin Flip Program

I'm trying to write a simple coin flip program and was wondering if I could get some help. I am fairly new to Java and was simply trying to ask the user how many times they would like to flip the coin. here is my code:

package cointossing;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
/**
 *  Coin tossing class to simulate the flip of a coin 
 *  with two sides; Heads and Tails.
 * 
 * @author Alex Chapman ID:
 *  
 */
public class CoinTossing 
{    
    public static String sideUp;
    public static int number;

      public void run()
    {
        try( Scanner input = new Scanner(in) ) 
        {
            out.print("Enter how many times you would like to flip the coin");
            out.print("if you enter 0 the program quits");
            int number = input.nextInt();    
        }
    }


    private static void coin() 
    {
        Random rand = new Random();
        int sideup = rand.nextInt(2);
        if (sideup == 0) 
        {
            sideUp = "heads";
        } 
        else 
        {
            sideUp = "tails";
        }
    }

    public static String getsideup() 
    {
        out.println(sideUp);
        return sideUp;
    }



    public static void main(String[] args) 
    {
        int hcount = 0;
        int tcount = 0;
        for (int i = 1; i <= number; i++) 
        {
            coin();
            if (getsideup().equals("heads")) 
            {
                hcount++;
            } 
            else 
            {
                tcount++;
            }
        }
        out.println("total heads = " + hcount + " total tails = " + tcount);
    }
}

but when i run the program it skips over asking the user anything and just displays 0's because there is no number of times to flip the coin...i feel like im on the right track but im stuck...any help would be greatly appreciated..

EDIT:

so in the interest of learning I decided to change up my program and change the coin to an enum and have the program return that enum value..i also changed the user input to 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 meld the two programs together so that all the new work as far as returning the enum value and such was still there but remove the 'menu' aspect and replace it with the ability for the user to input how many flips they would like to do from the previous program. here is the new 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 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..honestly I dont even know why im trying to do this if but for the knowledge aspect so if you dont wanna help a broski out i understand.

You are not calling run() in your main.

You will need to add run() before your for (int i = 1; i <= number; i++) call.

You will also need to check your variables again, it looks like you are using sideUp as an int and as a string. Try adding this.sideUp in your coin() call when you set the values "heads" and "tails" , or rename your int sideUp variable to avoid confusion.

Change your main method, like this:

public static void main(String[] args) 
    {
        int hcount = 0;
        int tcount = 0;
        Scanner sc = new Scanner(System.in);
        out.println("How many coin flips do you want?");
        int number = sc.nextInt();
        for (int i = 1; i <= number; i++) 
        {
            coin();
            if (getsideup().equals("heads")) 
            {
                hcount++;
            } 
            else 
            {
                tcount++;
            }
        }
        System.out.println("total heads = " + hcount + " total tails = " + tcount);
    }

Very short one

package BuildProjects;
import java.util.Random;
public class HeadsTails {

    public static void main(String[] args) {
        
        int coin ;
        
        // Random class
        
        Random rand = new Random();
        
        coin = rand.nextInt(4);
        
        if ( coin == 1) {
            System.out.println("Heads");
        }
        
        else 
            System.out.println("Tails");
    }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM