0

私の課題では、試行の入力を使用して、ソーダのキャップを開けて勝ったかどうかを調べて、賞を獲得した人の平均を決定するファイルを作成する必要があります。彼らが勝つチャンスは 5 分の 1 です。個別の試行の平均を取得したら、試行を読み戻して平均を計算します。ファイルから double を読み取ろうとすると問題が発生します。これはこれまでの私のコードです。

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

}

4

1 に答える 1

1

Stringa を読み込んで aに変換する方法を知りたければ、fromdoubleを使用するだけでa が返されます。次に、を使用してをに変換できます。または、 fromを使用できます。next()ScannerStringDouble.parseDouble(String s)StringdoublenextDouble()Scanner

例外処理をまだ学んでいるかどうかはわかりませんが、そうであれば、try-catchブロックを使用して可能性のある をキャッチできNumberFormatExceptionます。また、メソッドを習得している場合は、それらを使用する必要があります。つまり、メインのコードはできるだけ少なくする必要があります。メソッドを使用して、コードをよりきれいにすることができます。

編集:あなたが読んでいるトークンは ではないため、 InputMismatchExceptionfrom を取得しています。nextDouble()double

于 2013-11-01T00:57:08.537 に答える