该程序创建了一个名为 datafile.txt 的文件,并且应该使用文本 I/O 将随机创建的 100 个整数写入文件中。但是,我的输出是“java.util.Random@30c221”100 次。如何获得 100 个随机数?提前致谢。
import java.io.*;
import java.util.Random;
public class Lab5 {
public static void main(String args[]) {
//Open file to write to
try {
FileOutputStream fout = new FileOutputStream("F:\\IT311\\datafile.txt");
int index = 0;
//Convert FileOutputStream into PrintStream
PrintStream myOutput = new PrintStream(fout);
Random numbers = new Random();
//Declare array
int array[] = new int[100];
for (int i = 0; i < array.length; i++)
{
//get the int from Random Class
array[i] = (numbers.nextInt(100) + 1);
myOutput.print(numbers + " ");
}
}
catch (IOException e) {
System.out.println("Error opening file: " + e);
System.exit(1);
}
}
}