输入I / O作业中的JAVA错误(初学者)

时间:2014-10-27 01:50:40

标签: java file io printwriter

如果有五分之一的机会瓶盖有奖品,我必须编写一个程序,估算一个人必须喝的饮料瓶数才能赢得奖品。用户输入打开瓶子的试验次数,程序输出赢得奖品所用的平均瓶数,并在单独的文本文件中存储每次试验获胜的饮料量。

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;

public class BottleCapPrize2
{
public static void main (String[] args) throws IOException
{
   Scanner in = new Scanner (System.in);
   System.out.print("Number of Trials: ");
   int trials = in.nextInt();
   int trialNumber = 0;
   int total = 0;
   int average = 0;

   for (int x = 0; x < trials; x++){
      int counter = 0;
      double randNum = Math.random();
      trialNumber++;

       while (randNum < 0.8){
       counter ++;
       randNum = Math.random();
       }

     total += counter;
     average = (int) total/trials;
     PrintWriter outFile = new PrintWriter (new File ("bottleCap.txt"));
     outFile.println("Trial " + trialNumber + " took " + counter + " drinks.");
     outFile.close();

     if (trialNumber == trials){
     System.out.println("The average was " + average);

    }
   }    
 }

输出到编译器的程序部分工作正常,但输出到文本文件(bottleCap.txt)的部分无法正常工作。

当我打开bottleCap.txt时,就会出现这样的问题 &#34;试验1喝了3杯。&#34;,&#34;试验2喝了5杯。&#34;,....&#34;试验10喝了9杯。&#34;应该写。 相反,我只看到该程序的最后一次试验的输出。 (如果用户输入了10个试验,那么文本文件只写了#34;试验10需要3个饮料&#34;并且没有任何试用1-9)。

有人可以帮我解决问题吗?我现在看了几个小时,还没有进一步。谢谢!

**谢谢大家的帮助!这非常有帮助! **

4 个答案:

答案 0 :(得分:0)

使用FileWriter而不是PrintWriterappend标记设置为true

FileWiter outFile = new FileWriter(new File("bottleCap.txt"), true);

正在创建PrintWriter,将现有文件截断为零,因此以前的所有输出都将被删除。在追加模式下使用FileWriter会将输出附加到现有文件。

或者,您可以在循环外部移动PrintWriter的创建,从而防止多次创建并截断文件。

答案 1 :(得分:0)

将打开的文件操作移出循环,因此只打开一次以写入缓冲,然后将所有字符串刷新到文件。

 PrintWriter outFile = new PrintWriter (new File ("bottleCap.txt"));
 for (int x = 0; x < trials; x++){
  int counter = 0;
  double randNum = Math.random();
  trialNumber++;

   while (randNum < 0.8){
   counter ++;
   randNum = Math.random();
   }

 total += counter;
 average = (int) total/trials;  
 outFile.println("Trial " + trialNumber + " took " + counter + " drinks.");
 if (trialNumber == trials){
 System.out.println("The average was " + average);

}}
   outFile.close();

答案 2 :(得分:0)

问题是你每次都在创建一个新文件。对于每个循环,您正在创建一个文件并写入,然后您正在创建一个新的空文件然后写入。

因此您必须将循环编写器声明为循环

 PrintWriter outFile = new PrintWriter (new File ("bottleCap.txt"));

 for (int x = 0; x < trials; x++){
  int counter = 0;
  double randNum = Math.random();
  trialNumber++;

   while (randNum < 0.8){
   counter ++;
   randNum = Math.random();
   }

 total += counter;
 average = (int) total/trials;
 outFile.println("Trial " + trialNumber + " took " + counter + " drinks.");


 if (trialNumber == trials){
 System.out.println("The average was " + average);

  }
 outFile.close();    
  }

每次循环时你都不应该关闭文件

答案 3 :(得分:0)

建议使用BufferedWriterFileWriter的链,当使用其构造函数之一时,关键点是FileWriter会将String附加到当前文件中,以便通过添加来实现appaneding true最后一个参数

new FileWriter("bottleCap.txt", true)        

当我们用BufferedWriter对象包围它时为了更高效,如果要写入文件的时间,那么它将字符串缓存在大块中并将大块写入文件显然你可以节省大量的时间来写入文件

注意:有可能不使用BuffredWriter,但建议使用它,因为它具有更好的性能和缓冲大块字符串并将其写入一次的能力

只需更改

即可
PrintWriter out = new PrintWriter("bottleCap.txt"); 

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("bottleCap.txt", true)));

示例:

try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("bottleCap.txt", true)));){
            String data = " This content will append to the end of the file";
            File file =new File("bottleCap.txt");
        out.println(data);
            }catch(IOException e){
        }

可以在不使用BufferedWriter的情况下解决此问题,但是我提到的性能会很低。

示例:

try (PrintWriter out = new PrintWriter(new FileWriter("bottleCap.txt", true));) {
            String data = " This content will append to the end of the file";
            File file = new File("bottleCap.txt");
            out.println(data);

        } catch (IOException e) {
        }

<强>建议

  1. 将您的PrintWriter移出for循环,以避免多次生成bottleCap.txt个文件。

  2. 将这些行也移出for循环

    total += counter; average = (int) total/trials;