簡體   English   中英

在文件中讀取雙打

[英]Reading doubles in a file

對於我的作業,我要編寫一個文件,該文件使用試驗輸入來打開一個汽水蓋並查看他們是否獲勝,從而確定獲勝者的平均值。 他們有五分之一的獲勝機會。 一旦獲得了各個試驗的平均值,就可以讀回試驗並計算平均值。 我在嘗試從文件讀取雙打時遇到麻煩。 到目前為止,這是我的代碼。

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

}

如果您要詢問如何讀取String並將其轉換為double ,那么您所需要做的就是使用Scanner next() ,它返回String 然后,您可以使用Double.parseDouble(String s)String轉換為double 或者,您可以使用Scanner nextDouble()

不確定是否已經學會了異常處理,但是如果是這樣,則可以使用try-catch塊來捕獲可能的NumberFormatException 另外,如果您已經學會了方法,則應該使用它們,即,您的主代碼應盡可能少。 您可以使用方法使代碼更整潔。

編輯:您正在從nextDouble()獲取InputMismatchException ,因為您正在讀取的令牌不是double

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM