【问题标题】:Simple flip a coin Java Program简单的抛硬币Java程序
【发布时间】:2020-07-12 19:49:43
【问题描述】:

我正在使用一个随机数类并要求它将总正面数/翻转次数相加....但它不起作用。

任何提示都会有所帮助。

import java.util.Random;
public class FlipaCoin
{
    public static void main(String [] args)
    {
        Random rand= new Random();
        boolean a;
        while (a=true)
        {
            int flip=rand.nextInt(2);
            int heads=0;
            if (flip==1)
            {
                heads++;
            }
            double count=0;
            double percentage=heads/count;
            System.out.println(percentage);
            count++;
            if (percentage==50.0)
            {
                break;
            }
        }
    }
}

【问题讨论】:

  • 声明并初始化 heads 变量 above while 循环(不在其中)。就像现在一样,while 循环的每次迭代都会将 heads 变量设为 0。对 count 执行相同操作。

标签: java for-loop random logic counter


【解决方案1】:

我已经审查了您的程序并对其进行了修改,并将我的反馈留在了相关位置的 cmets 中。希望这篇评论对您有所帮助!

import java.util.*;

public class FlipaCoin {
  public static void main(String[] args) {
    Random rand = new Random();
    // don't
    // - boolean a;
    // If you're going to test a variable,
    // assign it a default value first
    boolean a = true;

    // Declaring these outside of the `while` loop below allows
    // them to exist through multiple executions of the loop's body
    int heads = 0;
    int count = 0;
    double percentage = 0;

    // It turns out that `percentage == 50.0` never evaluated to `true`, ever
    // Better to pick a limit beyond which
    int samples = 5000;

    // don't - while (a=true) {
    // Using the `=` operator assigns a value to a variable,
    // and this assignment itself results in a true, and loop forever
    // Using the `==` operator compares two operands for equality
    // of values, and returns true if same, false if different
    // This is how to test `a`.
    // while (a == true) {

    // We don't want it to loop forever though, so the condition was revised
    // `<=` is a "less than or equal to" comparison operator
    while (count <= samples) {
      int flip = rand.nextInt(2);
      // don't do this inside the loop
      // - int heads = 0;
      if (flip == 1) {
        heads++;
      }
      // don't do these inside the loop
      // - double count = 0;
      // - double percentage = heads / count;
      // Assigning a new value to a variable defined
      // outside the loop is what you want;

      // `(double) count` says "think of this value as a double" in just this line
      // but `count` stays int, so when logging below, it doesn't show as `105.00`
      percentage = heads / (double) count;

      // don't - System.out.println(percentage);
      // Give a little more context for what you're logging! :)
      System.out.println("Sample: " + count + "; Percent: " + percentage);

      count++;

      // don't, this will never succeed
      // - if (percentage == 50.0) {
      // -   break;
      // - }
    }
  }
}

【讨论】:

    【解决方案2】:

    查看 cmets:

    import java.util.Random;
    public class FlipaCoin
    {
        public static void main(String [] args)
        {
            Random rand= new Random();
            int heads=0; //you don't want to reset heads in every loop
            double count=0; //you don't want to reset heads in every loop
            //boolean a; //not needed
            while (true)
            {
                int flip=rand.nextInt(2);
                //int heads=0;
                if (flip==1)
                {
                    heads++;
                }
                count++;
                double percentage= heads/count*100 ;// heads/count give fraction, not percentage 
                if (percentage==50.0)
                {
                    System.out.println(percentage + "achived after "+ count +" flips");
                    break;
                }
            }
        }
    }
    

    你也可以使用rand.nextBoolean():

    public static void main(String [] args)
    {
        Random rand= new Random();
        int heads=0;
        double count=0;
        while (true)
        {
            boolean flip=rand.nextBoolean(); // true half the
            if (flip)
            {
                heads++;
            }
            count++;
            double percentage= heads/count*100;
            if (percentage==50.0)
            {
                System.out.println(percentage + "achived after "+ count +" flips");
                break;
            }
        }
    }
    

    它的简洁版本:

    public static void main(String [] args)
    {
            Random rand= new Random();
            int heads=0, count=0;
            do
            {
                heads = rand.nextBoolean() ? heads+1 : heads;
                count++;
            }while((double)heads+100/count!= 50);
            System.out.println("50% achived after "+ count +" flips");
    }
    

    【讨论】:

    • 一些 RNG 的低位不是很随机。以防万一我更倾向于使用int flip = rand.nextInt(10000) % 2; 来避免潜在的问题。 YMMV。
    猜你喜欢
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多