Java 如何抛硬币并显示反面和正面计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23019822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:04:47  来源:igfitidea点击:

How flipping a coin and display tails and heads counting?

javarandomcoin-flipping

提问by Fred882401

I am very beginner on Java, still getting passion about. I have been given this exercise: "Write a simulator program that flips a coin: One thousand times then prints out how many time you get tails and how many times you get heads" That is what i have tried to do so far.

我是 Java 的初学者,仍然对 Java 充满热情。我得到了这个练习:“编写一个抛硬币的模拟器程序:一千次,然后打印出你得到反面的次数和你得到正面的次数”这就是我到目前为止所尝试做的。

import java.util.Random; 
import java.util.regex.Pattern; 

public class coin { 

    public static void main( String [] args ) { 

        Random r = new Random(); 
        Pattern tail = Pattern.compile("Tail+"); 
        Pattern head = Pattern.compile("Head+"); 
        String flips = ""; 

        for (int i = 0; i < 1000; i++) { 
            flips += r.nextInt(100) % 2 == 0 ? "Head" : "Tail"; 
        } 
        String[] heads = head.split( flips ); 
        String[] tails = tail.split( flips ); 
        //Display
        System.out.println("Times head was flipped:" + heads.length); 
        System.out.println("Times tail was flipped:" + tails.length); 
    }
}

The program seems to be working, but it is giving me always an almost pair amount of heads and tails, which the total exceed 1000, at least by 1 or more. Please, someone has any solution of this? Where am I wrong? Thanks

该程序似乎正在运行,但它总是给我几乎一对数量的正面和反面,总数超过 1000,至少超过 1 个或更多。请问,有人对此有任何解决方案吗?我哪里错了?谢谢

采纳答案by JB Nizet

A coin has two sides, so I really don't see why you would ask the random generator to generate a number between 0 and 100 (exclusive). Between 0 and 2 (exclusive) would be much more logical.

一枚硬币有两个面,所以我真的不明白为什么你会要求随机生成器生成一个 0 到 100(不包括)之间的数字。在 0 和 2(不包括)之间会更合乎逻辑。

Also, you're being asked to count. Appending strings and then splitting to get the final value is quite a complex and inefficient way to count. You should use an integer instead. Each time you get a 1 from your random, increment a counter. In the end, you have the number of times 1 was returned, and the number of 0 is thus 1000 - this number.

另外,你被要求数数。附加字符串然后拆分以获得最终值是一种非常复杂且低效的计数方式。您应该改用整数。每次从随机数中获得 1 时,增加一个计数器。最后,您有返回 1 的次数,因此 0 的数量是 1000 - 这个数字。

    Random r = new Random(); 
    int heads = 0;
    for (int i = 0; i < 1000; i++) { 
        int side = random.nextInt(2);
        if (side == 1) {
            heads++;
        } 
    } 
    System.out.println("Times head was flipped:" + heads); 
    System.out.println("Times tail was flipped:" + (1000 - heads)); 

It could even be simplified to the follwoing (although this simplification makes the code a bit harder to understand):

它甚至可以简化为以下内容(尽管这种简化使代码更难理解):

    Random r = new Random(); 
    int heads = 0;
    for (int i = 0; i < 1000; i++) { 
        heads += random.nextInt(2);
    } 
    System.out.println("Times head was flipped:" + heads); 
    System.out.println("Times tail was flipped:" + (1000 - heads)); 

回答by Kakarot

Rather than appending the result in a String and then splitting the string and counting the occurence of "Head"/"Tail" you can just keep track of the count in separate variables :

而不是将结果附加到字符串中,然后拆分字符串并计算“头”/“尾”的出现,您只需跟踪单独变量中的计数:

int headCount = 0;
int tailCount = 0;

for (int i = 0; i < 1000; i++) {
    if(r.nextInt(100) %2 == 0)
    {
      headCount++;
    }
    else
    {
      tailCount ++;
    }

  System.out.println("Times head was flipped:" + headsCount); 
  System.out.println("Times tail was flipped:" + tailCount); 

}