简体   繁体   English

为什么这段代码写/打印所有 0.0 而不是不同的随机数?

[英]Why does this piece of code write/print all 0.0 instead of different random numbers?

I'm trying to generate random numbers and write them to a file, then print them.我正在尝试生成随机数并将它们写入文件,然后打印它们。 Instead all zeros such as this 0.0 are printed by line in the console and in the file its written in ASCII code.相反,所有像 0.0 这样的零都在控制台中按行打印,并在用 ASCII 代码编写的文件中打印。

I have already tied using different java input classes and using the random number generator.我已经使用不同的 java 输入类并使用随机数生成器进行绑定。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;


    public static void main(String[] args) throws IOException {
        makefile(10, "inputHW02.txt");

        double[] sizearray0 = new double[10];
        int i = 0;
        Scanner inFile;

        inFile = new Scanner(new File("inputHW02.txt"));

        while (inFile.hasNextDouble()) {
            sizearray0[i] = inFile.nextDouble();
            i++;
        }

        for (int r = 0; r < sizearray0.length; r++)
            System.out.println(sizearray0[r]);

        inFile.close();
    }

    public static void makefile(double n, String filename) throws IOException {

        FileOutputStream writer = new FileOutputStream(filename);
        DataOutputStream dos = new DataOutputStream(writer);
        Random rand = new Random();

        for (int i = 0; i < n; i++) {
            dos.writeInt((int) 60);
            ;

        }
        if (writer != null) {
            writer.flush();
            writer.close();
        }
    }

I expect the file and output console should have random numbers separated on a new line such as 10.0 23.0 1.0 and so forth.我希望文件和输出控制台应该在新行上分隔随机数,例如 10.0 23.0 1.0 等等。

The reason we use PrintWriter over DataOutputStream for writing to a file is better explained here .我们使用PrintWriter不是DataOutputStream来写入文件的原因在这里得到了更好的解释。

public static void makefile(double n, String fileName) throws IOException {
    FileWriter fileWriter = new FileWriter(fileName);
    PrintWriter printWriter = new PrintWriter(fileWriter);
    Random rand = new Random();

    for (int i = 0; i < n; i++) {
        printWriter.println(rand.nextDouble());  // <--- observe this
    }
    if (printWriter != null) {
        printWriter.flush();
        printWriter.close();
    }
}

Also,还,

  1. You were not using rand .你没有使用rand
  2. Use printWriter.println() that will take care of adding a newline character after each line is written to the file.使用printWriter.println()将负责在每行写入文件后添加换行符。

The DataInputStream / DataOutputStream classes read / write data in binary format. DataInputStream / DataOutputStream类以二进制格式读取/写入数据。 Whereas the Scanner reads the data in text format.而扫描器以文本格式读取数据。

Which means you are writing the binary equivalent of integer 60 to the file, and while reading, you are trying to read the character string "60".这意味着您正在将整数 60 的二进制等效项写入文件,并且在读取时,您正在尝试读取字符串“60”。

In short:简而言之:

  • Use DataInputStream if you are reading data written using DataOutputStream .如果您正在读取使用DataOutputStream写入的数据,请使用DataInputStream
  • Use Scanner or FileReader if you have used FileWriter for writing file.如果您使用FileWriter写入文件,请使用ScannerFileReader

Your code is not using rand.您的代码未使用 rand。 If I was in your situation, I would use a PrintWriter or BufferedWriter, it makes it simpler如果我处于你的情况,我会使用 PrintWriter 或 BufferedWriter,它会让事情变得更简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM