0
我似乎遇到此代碼的問題。目的是找出在5面模具上擲出1的平均嘗試次數。我想我有數學權利;我只是不能讓while
循環讀取文本文件的工作。閱讀文本文件時遇到問題
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.Random;
import java.util.Scanner;
public class BottleCapPrize
{
public static void main (String[] args) throws IOException
{
Random randy = new Random();
PrintWriter outFile = new PrintWriter(new File("boost.txt"));
Scanner inFile = new Scanner(new File("boost.txt"));
Scanner in = new Scanner(System.in);
int trials;
int tries = 6;
int winCap = 6;
int token = 0;
double average;
int total = 0;
System.out.print("Please enter the number of trials: ");
trials = in.nextInt();
for (int loop = 1; loop <= trials; loop++)
{
winCap = 6;
tries = 0;
while (winCap != 0)
{
tries++;
winCap = randy.nextInt(5);
}
outFile.println(tries);
System.out.println(tries);
}
while (inFile.hasNext())
{
token = inFile.nextInt();
total = total + token;
}
average = (double)total/(double)trials;
System.out.println("Average : " + average);
outFile.close();
inFile.close();
in.close();
}
}
什麼是你的電流輸出,什麼是您的文本文件的內容? – shinjw 2014-09-29 00:30:14
你爲什麼要用同一個文件讀寫? – 2014-09-29 05:14:42