读取文件中的双打

时间:2013-11-01 00:21:13

标签: java for-loop file-io readfile

对于我的任务,我要写一个文件,该文件使用试验输入来确定赢得奖品的人的平均值,打开汽水瓶盖并查看是否赢了。他们有五分之一的机会获胜。一旦我获得了单独试验的平均值,我将回读试验并计算平均值。我在尝试从文件中读取双打时遇到了麻烦。到目前为止,这是我的代码。

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);
}

}

1 个答案:

答案 0 :(得分:1)

如果您询问如何阅读String并将其转换为double,那么您需要做的就是使用next()中的Scanner返回String。然后,您可以使用Double.parseDouble(String s)String转换为double。或者您可以使用nextDouble()中的Scanner

不确定您是否已学习异常处理,但如果是,则可以使用try-catch块来捕获可能的NumberFormatException。此外,如果你已经学会了方法,你应该使用它们,即你的主要代码应该尽可能少。您可以使用方法使代码更清晰。

编辑: 您从InputMismatchException获得nextDouble(),因为您正在阅读的令牌不是double