Javaの知識が不足していることをあらかじめお詫びします。私はJavaプログラミングが初めてで、コインを投げて、コインがN回のロール以内に表に出た回数を数え、そうするのにかかる時間を測定し、それを印刷するプログラムを作成しようとしています. .txt ファイルに保存できるようにします。私はほとんどそれを手に入れたと思います。ただ今、印刷するのに苦労しています。どんな助けでも大歓迎です!私は立ち往生しています!
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();
}
}