将随机数组输出到文件
我正在尝试编写一个程序,该程序采用 200 个数字 (1-200) 的数组,将它们随机化,然后将这些数字输出到文本文件。
我一整天都在挣扎,但我不明白为什么没有任何效果。
主要方法:
public static void main (String[] args)
{
int[] numbers= new int [201];
for (int i=0; i < numbers.length; i++)
{
numbers[i]=i;
}
}//end main method
随机化方法:
public static int[] randomizeArray(int[] numbers)
{
Random gen= new Random(10);
for (int i=0; i < numbers.length; i++)
{
int n= gen.nextInt(200);
numbers[i]=n;
}
return numbers;
}//end randomizeArray method
和打印方法:
public static int[] outputArray(int[] numbers) throws IOException
{
FileOutputStream output;
output= new FileOutputStream("RandomOut.txt");
new PrintStream(output).println(randomizeArray(numbers));
output.close();
return numbers;
}//end method outputArray
任何帮助都会很棒,我知道我忽略了某些事情或做错了事情。
I'm trying to write a program that takes an array of 200 numbers (1-200), randomizes them, and then outputs those numbers to a text file.
I've been struggling for the whole day and I can't figure out why nothing is working.
Main method:
public static void main (String[] args)
{
int[] numbers= new int [201];
for (int i=0; i < numbers.length; i++)
{
numbers[i]=i;
}
}//end main method
Randomize method:
public static int[] randomizeArray(int[] numbers)
{
Random gen= new Random(10);
for (int i=0; i < numbers.length; i++)
{
int n= gen.nextInt(200);
numbers[i]=n;
}
return numbers;
}//end randomizeArray method
And the print method:
public static int[] outputArray(int[] numbers) throws IOException
{
FileOutputStream output;
output= new FileOutputStream("RandomOut.txt");
new PrintStream(output).println(randomizeArray(numbers));
output.close();
return numbers;
}//end method outputArray
Any help would be great, I know I'm overlooking something or doing something incorrectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不应该在 main 方法的末尾调用 outputArray 吗?
Shouldn't you call outputArray at the end of your main method?
您的问题之一是这一行:
这可能会打印类似:
是的?您需要编写一个 for 循环来输出数字,类似于:
只不过您不希望每次在循环中都创建 PrintStream。
One of your problems is the line:
This will probably print something like:
yes? You need to write a for loop to output the numbers, something like:
except that you don't want to create the PrintStream each time in the loop.
您的 main 方法初始化一个包含 201 个元素(而不是 200 个)的数组,并且不对这个数组执行任何操作。显然,没有随机化,现在写入任何文件。 main 方法应调用
randomizeArray
,然后调用outputArray
。main 中数组元素的初始化是无用的,因为这些元素将由 randomizeArray 方法重新初始化。顺便说一下,这个方法不需要返回任何内容。
最后,
outputArray
方法应循环遍历数组并 println 每个元素。流应该在finally 块中关闭。它也不应该返回任何内容。Your main method initializes an array of 201 elements (instead of 200), and doesn't do anything with this array. So obviously, there is no randomization and now writing to any file. The main method should call
randomizeArray
and thenoutputArray
.The initialization of the array elements in main is useless, since the elements will be reinitialized by the
randomizeArray
method. This method, by the way, doesn't need to return anything.Finally, the
outputArray
method should loop through the array and println each element. The stream should be closed in a finally block. It should not return anything either.1)您需要使用 Arrays.toString(int[] arr) 来打印该数组。
2)如果您的意思是重新排序数组输入,则需要非常不同的代码。
否则,删除输入并使用新数组。
3)调用你的辅助方法!
编辑:添加了这个伪代码:
1) you need to use
Arrays.toString(int[] arr)
to print that array.2) if you mean to reorder the array input, that requires code tht is very diferent.
otherwise, get rid of the input and use a new array.
3) call your helper methods!
EDIT: added this psuedocode:
无法抗拒...
Couldn't resist...