Java 读取文件中的双精度

Java 读取文件中的双精度,java,for-loop,file-io,readfile,Java,For Loop,File Io,Readfile,在我的作业中,我要写一个文件,通过打开一个苏打水瓶盖,看看他们是否中奖,使用试验输入来确定中奖者的平均值。他们有五分之一的机会获胜。一旦我有了单独试验的平均值,我就要把试验读回并计算平均值。我很难从文件中读出双倍的数字。这是到目前为止我的代码 import java.util.Scanner; import java.io.File; import java.io.PrintWriter; import java.io.IOException; public class BottleCapPri

在我的作业中,我要写一个文件,通过打开一个苏打水瓶盖,看看他们是否中奖,使用试验输入来确定中奖者的平均值。他们有五分之一的机会获胜。一旦我有了单独试验的平均值,我就要把试验读回并计算平均值。我很难从文件中读出双倍的数字。这是到目前为止我的代码

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);
}
import java.util.Scanner;
导入java.io.File;
导入java.io.PrintWriter;
导入java.io.IOException;
公共级瓶装水
{
公共静态void main(字符串[]args)引发IOException
{
双平均瓶=0.0;
双随机数=0.0;
int和=0;
int numBottles=1;
int=0;
int maxRange=6;
int minRange=1;
int-oneTrial=0;
int-winPrize=1;
双令牌=0;
int totalSumCaps=0;
扫描仪输入=新扫描仪(系统输入);
//为第一次测试生成瓶号
randNum=Math.random();
瓶子=((int)((randNum)*(maxRange-minRange))+minRange);
//构造一个名为outFile的对象,以允许访问PrintWriter类的输出方法
PrintWriter outFile=新的PrintWriter(新文件(“trials.txt”);
//获取用户输入的试用次数
System.out.print(“输入试验量:”);
int trials=in.nextInt();
System.out.println();
//检查输入试验的平均值

对于(int loop=1;loop如果您询问如何读入
字符串
并将其转换为
double
,那么您需要做的就是使用
Scanner
中的
next()
,它返回一个
字符串
。然后您可以使用
double.parseDouble(字符串s)
将您的
字符串
转换为
双精度
。或者您可以使用
扫描仪中的
nextDouble()

不确定您是否已经学习了异常处理,但如果是这样,您可以使用
try catch
块来捕获可能的
NumberFormatException
。此外,如果您已经学习了方法,您应该使用它们,也就是说,您的main中应该有尽可能少的代码。您可以使用方法使代码更干净

编辑:
您从
nextDouble()
中获得了
inputmaschetchException
,因为您正在读取的令牌不是
double

请看“有问题”是什么意思?出了什么问题?您知道int/double默认为0/0.0,对吗?那么nextDouble(“试用123.0.65739”)是什么意思要告诉你吗?我猜不会是双精度的…当程序到达token=infle.nextDouble()时,它会生成这个“java.util.InputMismatchException:null(在java.util.Scanner中)@EpicbloodI,我只是取出了文件的字符串部分,留下了双精度部分,谢谢!