Home > OS >  Keeping a tally of how many times a number appears using a random number generator. Java
Keeping a tally of how many times a number appears using a random number generator. Java

Time:11-24

In this code I am writing here the user inputs whether or not they would like to choose heads or tails in a coinflip game. I would like to keep a tally of how many times heads appears or tails appears and output it each time it changes. After hours of trying and searching I cannot figure it our perfectly so if someone could let me know what I could utilize let me know.

import java.util.Random;
import java.util.Scanner;

public class CoinToss {


    private enum Coin {
        Head, Tail
    }

    public static void main(String[] args) {
        CoinToss game = new CoinToss();
        game.startGame();


    }


    private void startGame() {
        Scanner scanner = new Scanner(System.in);
        Coin guess;

        while (true) {
            System.out.print("Enter your guess whether the coin will be heads or tails. Type 1 for heads, 2 for tails, or 3 to quit: ");
            String choice = scanner.nextLine();

            if (choice.equalsIgnoreCase("3")) {
                break;
            } else if (choice.equalsIgnoreCase("1")) {
                guess = Coin.Head;
            } else if (choice.equalsIgnoreCase("2")) {
                guess = Coin.Tail;
            } else {
                System.out.println("Please select either heads tails or quit.");
                continue;
            }

            Coin toss = tosscoin();

            if (guess == toss) {
                System.out.println("You guessed correctly!");
            } else {
                System.out.println("You guessed incorrectly");
            }
        }
        scanner.close();
    }


    private Coin tosscoin() {
        
        Random r = new Random();
        int sideup = r.nextInt(2);
        if (sideup == 1) {
            
            return Coin.Head;
        } else {
            return Coin.Tail;
        }


    }

}

CodePudding user response:

You can for example add two fields in your CoinToss class. Like int heads and int tails. Initialize them with 0 in the startGame() method. Then, in the tosscoin() method:

 if (sideup == 1) {
    heads  ;
    return Coin.Head;
} else {
    tails  ;
    return Coin.Tail;
}

You can access these fields in the startGame() method and do whatever you want with them.

You could as well define these two variables directly in the startGame() method and increment them based on the type of Coin which you get from the tosscoin() method.

CodePudding user response:

Below code should work. everytime it tosses, it stores the current value in a variable and compares it next time with the toss value.


import java.util.Random;
import java.util.Scanner;

public class CoinToss {

    private static int headCounter;
    private static int tailCounter;
    private static int previousToss;

    private enum Coin {
        Head, Tail
    }

    public static void main(String[] args) {
        CoinToss game = new CoinToss();
        game.startGame();


    }


    private void startGame() {
        headCounter = 0;
        tailCounter = 0;
        previousToss = 0;
        Scanner scanner = new Scanner(System.in);
        Coin guess;

        while (true) {
            System.out.print("Enter your guess whether the coin will be heads or tails. Type 1 for heads, 2 for tails, or 3 to quit: ");
            String choice = scanner.nextLine();

            if (choice.equalsIgnoreCase("3")) {
                break;
            } else if (choice.equalsIgnoreCase("1")) {
                guess = Coin.Head;
            } else if (choice.equalsIgnoreCase("2")) {
                guess = Coin.Tail;
            } else {
                System.out.println("Please select either heads tails or quit.");
                continue;
            }

            Coin toss = tosscoin();

            if (guess == toss) {
                System.out.println("You guessed correctly!");
            } else {
                System.out.println("You guessed incorrectly");
            }
        }
        scanner.close();
    }


    private Coin tosscoin() {

        Random r = new Random();
        int sideup = r.nextInt(2);
        Coin currentGuess;

        if (sideup == 1) {
            headCounter  ;
            currentGuess = Coin.Head;
        } else {
            tailCounter  ;
            currentGuess = Coin.Tail;
        }
        checkIfFlipped(sideup);
        return currentGuess;

    }

    static void checkIfFlipped(int currentToss) {
        
        if (currentToss != previousToss) {
            
            if (currentToss == 0) {
                System.out.println("Coin fliped from head to tail");
            } else {
                System.out.println("Coin fliped from tail  to head");
            }
        }

        previousToss = currentToss;
    }
}
  • Related