【问题标题】:File to hold 500 random numbers ranging from 0 to 1000. Writes these numbers to the file then open the file and read the numbers one by one文件保存 500 个从 0 到 1000 的随机数。将这些数字写入文件,然后打开文件并一一读取数字
【发布时间】:2013-03-22 23:50:51
【问题描述】:

在 while 循环中发现困难。我不确定如何开始调用和/或读取生成文件中的每个数字,以便确定最小和最大数字以及总和。另外,如果可能的话,有人可以解释如何编写代码,以便如果任何数字是连续的,它就会计算多少次。

PrintWriter prw = new PrintWriter("results.txt");
int num, largest, smallest, total = 0, count = 0;
int programnumber = 6;
    double average = 0;

PrintWriter prw1= new PrintWriter(new FileWriter("randomdata.txt"));
Random generator = new Random(); 
for (int i = 0; i < 1001; i++){     
         num = generator.nextInt(500);      //Will this generate a file w/ 500 num?
         prw1.write(num + "\n");
    }
    prw1.close();

    largest = 0;        
    smallest = 9999;        
    while (prw1.nextInt()){            //what call statement do I use? 
            num = (prw1.nextInt());    //unsure how to begin reading numbers
            if (num > largest){
                largest = num;
            }
            if (num < smallest){
                smallest = num;
            }
    total += num;
    count++; 
        }
    average = (total / total);

【问题讨论】:

  • 您应该为变量使用驼峰式大小写——programNumber、randomData.txt 等...
  • 您的循环还会添加 1001 个数字,而不仅仅是 500 个带问号的评论。记住 java 是基于 0 的......所以
  • 谢谢。尽管创建文件并使用while循环分别从文件中调用/读取每个数字以确定最大值和最小值,但我都特别挣扎。
  • @matthewcrandall 我建议一次考虑一个部分。 1.首先尝试生成您需要的随机数。 2.然后将它们存储在文件中。 3. 使用扫描器类打开文件。只需做一些谷歌搜索就会有大量的例子。

标签: java arrays file loops random


【解决方案1】:
num = generator.nextInt(500);      //Will this generate a file w/ 500 num?

上面做的是生成一个0到499之间的随机值

为了让您从文件中读取,您必须使用BufferedReaderScannerFileReader 或任何其他阅读器,并且不要使用PrintWriter,因为它用于写入,而不是用于读取。

所以你可以试试这个。首先创建一个阅读器:

Scanner scr = new Scanner(fileToRead); //fileToRead should be the file you wrote

然后替换如下:

while (prw1.nextInt()){            //what call statement do I use? 
    num = (prw1.nextInt());    //unsure how to begin reading numbers
    // ...
}

用这个:

while(scr.hasNextLine()){
    num = Integer.parseInt(scr.nextLine());
    // ...
}

【讨论】:

    【解决方案2】:

    我认为nextInt 不会像你认为的那样做。你写:

    num = generator.nextInt(500); //Will this generate a file w/ 500 num?
    

    这个问题的答案是。根据the Random documentation

    public int nextInt(int n)  
    

    均匀地返回一个伪随机数 分布在 0(含)和指定值之间的 int 值 (独家),从这个随机数生成器的序列中提取。

    所以nextInt(500) 会生成一个介于 0 和 499(含)之间的数字。

    相反,您需要使用 nextInt(1000 + 1) 来获取 0 到 1000 之间的数字,包括 0 到 1000。

    您还应该更改阅读代码。您正在尝试从输出流中读取,这是您无法做到的。您可以更改代码以使用扫描仪,但我个人会使用BufferedReader

    try {
        BufferedReader br = new BufferedReader(new FileReader("randomdata.txt"));
        String line = br.readLine();
        while (line != null) {
            // Do something, e.g. Integer.parseInt(line);
            line = br.readLine();
        }
        br.close();
    } catch (IOException ie) {
        ie.printStackTrace();
    }
    

    使用BufferedReader 而不是Scanner 的一个原因是它可以扩展到不同的数据格式。也许在一行中的每个数字之前都有一个前缀,或者一行中有两个数字。您可以使用BufferedReader 获取整行,然后在解析之前格式化字符串。使用Scanner,在"Number: 6" 上调用nextInt 将无法正常工作。

    【讨论】:

    • 1.如果第一行不为空,您的 while 循环将永远不会终止。 2.在将整数保存到文件时,您需要提供一些空间。 3. 读取文件的Scanner类更适合这种情况。
    • @Smit:感谢您的评论;忘记分配nextLine 的结果。通过将ints 保存到文件来提供空间是什么意思?在我的示例中,我没有对写作做任何事情。另外,我一般使用BufferedReaders 而不是Scanners;你能解释一下为什么Scanner 会“更适合……更适合这种情况”吗?
    • 在保存文件时,您需要提供一些分隔符,否则所有内容都会合并在一起。扫描仪将适合,因为扫描仪类有方法 hasNextInt()nextInt() 可以避免解析。
    • 我写了我的代码来匹配原始海报的代码。在问题中,他使用分隔符"\n",所以我使用BufferedReader 来读取每一行。我没有修改任何编写代码;我只是使用了他提供的分隔符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多