2013-11-01 41 views
0

對於我的任務,我要編寫一個文件,其中使用輸入試驗來確定打開蘇打水帽並獲得獎品的人獲得獎品的平均值。他們有五分之一的機會獲勝。一旦我有單獨的試驗的平均值,我將讀回試驗並計算平均值。我有麻煩試圖從文件中讀取雙打。這是我的代碼到目前爲止。在文件中讀取雙打

import java.util.Scanner; 
import java.io.File; 
import java.io.PrintWriter; 
import java.io.IOException; 
public class BottleCapPrize 
{ 
public static void main(String[] args) throws IOException 
{ 
    double averageBottles = 0.0; 
    double randNum = 0.0; 
    int bottleSum = 0; 
    int numBottles = 1; 
    int bottle = 0; 
    int maxRange = 6; 
    int minRange = 1; 
    int oneTrial = 0; 
    int winPrize = 1; 
    double token = 0; 
    int totalSumCaps = 0; 
    Scanner in = new Scanner(System.in); 

    //Generates bottle number for first test 
    randNum = Math.random(); 
    bottle = ((int)((randNum) * (maxRange - minRange)) + minRange); 

    //construct an object called outFile to allow access to output methods of the PrintWriter class 
    PrintWriter outFile = new PrintWriter(new File("trials.txt")); 

    //Gets users input for how many trials 
    System.out.print("Enter the amount of trials: "); 
    int trials = in.nextInt(); 
    System.out.println(); 

    //Check averages for entered trials 
    for (int loop = 1; loop <= trials; loop++)  
    { 
     //Clears out the loops variables each time through 
    if(loop != 0) 
    { 
     averageBottles = 0; 
     bottleSum = 0; 
     oneTrial = 0; 
     numBottles = 0; 
    } 
    for(int x = 1; x <= 20; x++) 
    { 
     if(x == 20) //One trial is completed 
     { 
     averageBottles = bottleSum/x; 
     //Replaces the old bottle number for a new bottle 
     randNum = Math.random(); 
     bottle = ((int)((randNum) * (maxRange - minRange)) + minRange); 
     } 
     else if(bottle == winPrize) 
     { 
     oneTrial = numBottles; 
     if(oneTrial == 0) 
     { 
      oneTrial = 1; 
     } 
     //Replaces the old bottle number for a new bottle 
     randNum = Math.random(); 
     bottle = ((int)((randNum) * (maxRange - minRange)) + minRange); 
     } 
     else if(bottle != winPrize) //not a winner, gets new bottle and increments the number of bottles tested 
     { 
     //Replaces the old bottle number for a new bottle 
     randNum = Math.random(); 
     bottle = ((int)((randNum) * (maxRange - minRange)) + minRange); 
     oneTrial = numBottles; 
     numBottles ++; 
     } 
     bottleSum += oneTrial; //Adds the sum from each trial 
    } 
    outFile.println("Trial " + loop + "." + " " + averageBottles); //Prints the averages to the file 
    System.out.println("Trial " + loop + "." + " " + averageBottles); 
    //outFile.println("Trial " + "=" + " " + averageBottles); //Prints the averages to the file 
    //System.out.println("Trial "+ "=" + " " + " " + averageBottles); 
    }//end of for loop 
    outFile.close ();//close the file when finished 

    //Read the trial data back in and calculate the average 
    File fileName = new File("trials.txt"); 
    Scanner inFile = new Scanner(fileName); 

    //read file and get data 
    while(inFile.hasNext()) 
    { 
    token = inFile.nextDouble(); 
    totalSumCaps += token; 
    } 
    inFile.close(); 

    double totalAverageCaps = totalSumCaps/trials; 
    //Print results 
    System.out.println(); 
    System.out.println("The average number of caps opened in order to win a prize is: " + totalAverageCaps); 
} 

}

+0

請參閱['Double.parseDouble()'](http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble(java.lang.String)) – Justin

+0

「有麻煩」是什麼意思?發生了什麼問題? – Epicblood

+0

你知道整數/雙打默認爲0/0.0吧? – Tdorno

回答

1

如果你問如何在String讀取,並將其轉換爲double,那麼所有你需要做的是使用next()Scanner,它返回一個String。然後您可以使用Double.parseDouble(String s)將您的String轉換爲double。或者您可以使用nextDouble()Scanner

不知道您是否已經學習了異常處理,但如果是這樣,您可以使用try-catch塊來捕獲可能的NumberFormatException。另外,如果你已經學會了方法,你應該使用它們,也就是說,你應該在你的主體中儘量少用代碼。你可以使用方法來使你的代碼更加清潔。

編輯: 你從nextDouble()得到一個InputMismatchException因爲你正在閱讀的標記不是double

+0

我剛剛拿出字符串文件的一部分,並離開雙重部分,並工作,謝謝! – user2922247