简体   繁体   English

连续抛硬币

[英]consecutive coin flip

I apologize in advance for my lack of Java knowledge. 我因缺乏Java知识而向您致歉。 I am new to Java programming and am trying to make a program where I can flip a coin and count how many times the coin lands on heads within N amount of rolls, measure the time it takes to do so, and then print it out in the Console so that I can save it in a .txt file. 我是Java编程的新手,正在尝试制作一个程序,在其中我可以掷硬币并计算N次滚动中硬币落在头上的次数,测量花费的时间,然后打印出来。控制台,以便我可以将其保存为.txt文件。 I think I've almost gotten it; 我想我已经快到了。 I'm just having difficulties printing it out now. 我现在很难打印出来。 Any help would be appreciated! 任何帮助,将不胜感激! I'm stuck! 我被卡住了!

import java.util.Random;
import java.io.*;

public class RollGraph
{
    public static void flip(int n)
    {
        Random rnd = new Random();
        int roll = 0;
        int countHeads = 0;
        int headsInRow = 0;
        int headsOrTails = rnd.nextInt(2);

        while(roll<n){

            if(headsOrTails == 1){
                countHeads++;
                headsInRow++;
            }
            else{
                headsInRow=0;
            }
        }
        return;
    }

    public static void main(String[] arg) throws IOException
    {
        BufferedWriter writer = new BufferedWriter(
                new FileWriter( new File("data.txt")));
        long start,end,elapsed;
        int repeat = 20;
        double total;
        double average;

        for(int n=1;n<100;n++)
        {
            total = 0.0;
            for(int j=0;j<repeat;j++)
            {
                start = System.nanoTime();
                flip(n);
                end = System.nanoTime();
                elapsed = end - start;
                total += elapsed/1000000;
            }
            average = total/repeat;
            String line = n+"\t"+ average+"\t"+Math.log(average);
            System.out.println(line);
            writer.write(line);
            writer.newLine();
            writer.flush();
        }
        writer.close();
    }
}

In the flip method in this loop while(roll<n){ , 在此循环的flip方法中while(roll<n){
here you don't increment the roll variable. 在这里,您无需增加roll变量。

This is one problem I see. 我看到这是一个问题。

Check the logic of your flip method. 检查flip方法的逻辑。 Does not seem right to me. 在我看来不对。

I see two problems. 我看到两个问题。 First, you have an infinite loop in flip , because you don't increment roll at all. 首先,在flip有一个无限循环,因为根本不增加roll Increment it. 增加它。

Second, you have integer division on this line: 其次,您在此行上有整数除法:

total += elapsed/1000000;

In Java, dividing two int s must result in an int , so you will probably get a bunch of zeroes here. 在Java中,将两个int相除必须得到一个int ,因此在这里您可能会得到一堆零。 Use a double literal or cast elapsed to double to perform floating-point arithmetic. 使用double字面或浇铸elapseddouble执行浮点运算。

total += elapsed/1000000.0;

OR 要么

total += (double) elapsed/1000000;

除了已经提到的无限循环和除法问题之外,如果您想使打印线更整洁,我建议使用BigDecimal将双精度取整:

String line = n+"\t"+ BigDecimal.valueOf(average).setScale(5, BigDecimal.ROUND_HALF_UP) + "\t"+ BigDecimal.valueOf(Math.log(average)).setScale(5, BigDecimal.ROUND_HALF_UP);

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

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