【问题标题】:Keeping a tally of how many times a number appears using a random number generator. Java使用随机数生成器记录一个数字出现的次数。爪哇
【发布时间】:2022-01-02 09:01:15
【问题描述】:

在我在这里编写的这段代码中,用户输入他们是否想在掷硬币游戏中选择正面或反面。我想记录出现正面或反面的次数,并在每次变化时输出。经过数小时的尝试和搜索,我无法完美地理解它,所以如果有人能让我知道我可以利用什么,请告诉我。

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


    }

}

【问题讨论】:

  • 这不起作用怎么办?
  • 它正在工作,如果我没有把我的问题说清楚,对不起。当用户继续选择时,我试图记录每边出现的次数。
  • 很抱歉,您在这里所做的似乎主要揭示了计算机上“非随机”随机数是如何从真正随机的结果中产生的。数学/统计“二项式分布”(又名钟形曲线)是这个问题的真正随机(非计算机随机)答案

标签: java for-loop random


【解决方案1】:

下面的代码应该可以工作。每次投掷时,它会将当前值存储在一个变量中,并在下次与投掷值进行比较。


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

【讨论】:

  • 非常感谢我在正确的轨道上但并不完美,这是完美的指导。
【解决方案2】:

例如,您可以在 CoinToss 类中添加两个字段。像 int headint tails。在 startGame() 方法中将它们初始化为 0。然后,在 tosscoin() 方法中:

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

您可以在 startGame() 方法中访问这些字段,并对它们做任何您想做的事情。

您也可以直接在 startGame() 方法中定义这两个变量,并根据您从 tosscoin() 方法获得的 Coin 类型递增它们.

【讨论】:

  • 谢谢!这让我走上了正确的道路,让我对我需要做什么有了一个好主意
猜你喜欢
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2023-01-03
  • 2011-12-29
  • 1970-01-01
  • 2020-03-05
相关资源
最近更新 更多