简体   繁体   English

抛硬币 Java

[英]Coin flip in Java

I'm currently taking an introduction to java class in college and this particular assignment has me stumped.我目前正在大学介绍 java class,这个特殊的作业让我感到难过。 The goal is to “simulate flipping a coin (0 is heads and 1 is tails) 5 times.目标是“模拟抛硬币(0 为正面,1 为反面)5 次。 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.”它还指出“使用带有用户定义输入的随机 object,为掷硬币生成 0 或 1。”

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.到目前为止,我的代码采用输入的种子,并在用户猜测它是什么之后随机化一个介于 0 和 1 之间的数字。 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!");
}    

Consider using a for-loop to simplify the code:考虑使用 for 循环来简化代码:

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);

for (int i = 1; i <= 5; i++) {
    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 the for statement works.您可以访问此链接以详细了解for语句的工作原理。

If you want to make your code better, you'll need to use a function. So basically, put this code.如果你想让你的代码更好,你需要使用 function。所以基本上,把这个代码。

    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.进入这个 function。这个想法是,你正在重新使用这个代码,而不是一遍又一遍地输入它。

private static void getUserGuessForTrial(int trial) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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