Home > Enterprise >  Coin flip in Java
Coin flip in Java

Time:03-05

I'm currently taking an introduction to java class in college and this particular assignment has me stumped. The goal is to “simulate flipping a coin (0 is heads and 1 is tails) 5 times. For each flip, the user guesses the status of the flip. The computer reports if the status and evaluates the guess after each flip and provides a cumulative report before termination.” It also states “Using a Random object seeded with a user-defined input, generate a 0 or 1 for the coin toss.”

So far the code that I have takes the inputted seed and randomizes a number between 0 and 1 after the user guesses which it’ll be. But it looks so messy and repetitive. How can I simplify this?

Scanner scanner = new Scanner(System.in);
  
System.out.println("Enter the seed for the Random object.");
int seed = scanner.nextInt(); //Saves input as seed

Random coin = new Random(seed);
int guess = coin.nextInt(2);

System.out.println("What is your guess for trial # 1?");
int trialOne = scanner.nextInt();

System.out.println("The coin toss was a "   guess  ".");

if (guess == trialOne) {
    System.out.println("You guessed right!");
} else {
    System.out.println("Sorry, try again next time!");
}

Random coin2 = new Random(seed);
int guess2 = coin2.nextInt(2);

System.out.println("What is your guess for trial # 2?");
int trialTwo = scanner.nextInt();

System.out.println("The coin toss was a "   guess  ".");

if (guess == trialTwo) {
    System.out.println("You guessed right!");
} else {
    System.out.println("Sorry, try again next time!");
}

Random coin3 = new Random(seed);
int guess3 = coin3.nextInt(2);

System.out.println("What is your guess for trial # 3?");
int trialThree = scanner.nextInt();

System.out.println("The coin toss was a "   guess  ".");

if (guess == trialThree) {
    System.out.println("You guessed right!");
} else {
    System.out.println("Sorry, try again next time!");
}

Random coin4 = new Random(seed);
int guess4 = coin4.nextInt(2);

System.out.println("What is your guess for trial # 4?");
int trialFour = scanner.nextInt();

System.out.println("The coin toss was a "   guess4  ".");

if (guess == trialFour) {
    System.out.println("You guessed right!");
} else {
    System.out.println("Sorry, try again next time!");
}

Random coin5 = new Random(seed);
int guess5 = coin5.nextInt(2);

System.out.println("What is your guess for trial # 5?");
int trialFive = scanner.nextInt();

System.out.println("The coin toss was a "   guess5  ".");

if (guess == trialFive) {
    System.out.println("You guessed right!");
} else {
    System.out.println("Sorry, try again next time!");
}    

CodePudding user response:

Consider using a for-loop to simplify the code:

Scanner scanner = new Scanner(System.in);
  
System.out.println("Enter the seed for the Random object.");
int seed = scanner.nextInt(); //Saves input as seed

for (int i = 1; i <= 5; i  ) {
    Random coin = new Random(seed);
    int guess = coin.nextInt(2);

    System.out.println("What is your guess for trial #"   i   "?");
    int trial = scanner.nextInt();

    System.out.println("The coin toss was a "   guess   ".");

    if (guess == trial) {
        System.out.println("You guessed right!");
    } else {
        System.out.println("Sorry, try again next time!");
    }
}

You were doing repetitive task, which followed the same pattern. So, you can easily simplify such code using loop. You can visit this link to learn in details about how for statement works.

CodePudding user response:

If you want to make your code better, you'll need to use a function. So basically, put this code.

    Random coin2 = new Random(seed);
        int guess2 = coin2.nextInt(2);
    
        System.out.println("What is your guess for trial # 2?");
        int trialTwo = scanner.nextInt();
    
        System.out.println("The coin toss was a "   guess  ".");
    
        if (guess == trialTwo) {
            System.out.println("You guessed right!");
        } else {
            System.out.println("Sorry, try again next time!");
        }

Into this function. The idea being, you're re-using this code, rather than typing it out over and over again.

private static void getUserGuessForTrial(int trial) {

}
  •  Tags:  
  • java
  • Related