将随机数组输出到文件

发布于 2024-12-19 04:39:19 字数 968 浏览 2 评论 0原文

我正在尝试编写一个程序,该程序采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

宫墨修音 2024-12-26 04:39:19

你不应该在 main 方法的末尾调用 outputArray 吗?

Shouldn't you call outputArray at the end of your main method?

橘寄 2024-12-26 04:39:19

您的问题之一是这一行:

new PrintStream(output).println(randomizeArray(numbers));

这可能会打印类似:

[I@10769dd

是的?您需要编写一个 for 循环来输出数字,类似于:

for (int i=0; i < numbers.length; i++) {
    new PrintStream(output).println(numbers[i]);
}

只不过您不希望每次在循环中都创建 PrintStream。

One of your problems is the line:

new PrintStream(output).println(randomizeArray(numbers));

This will probably print something like:

[I@10769dd

yes? You need to write a for loop to output the numbers, something like:

for (int i=0; i < numbers.length; i++) {
    new PrintStream(output).println(numbers[i]);
}

except that you don't want to create the PrintStream each time in the loop.

剧终人散尽 2024-12-26 04:39:19

您的 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 then outputArray.

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.

只为一人 2024-12-26 04:39:19

1)您需要使用 Arrays.toString(int[] arr) 来打印该数组。

2)如果您的意思是重新排序数组输入,则需要非常不同的代码。
否则,删除输入并使用新数组。

3)调用你的辅助方法!

编辑:添加了这个伪代码:

boolean[] used=new boolean[200];
make old[] and new[]
for(i=0;i<200;i++){
    int n=random number from 0 to 199;
    while(used[n]) n=(n+1)%200;
    new[i]=old[n];
    used[n]=true;
}
return new;

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:

boolean[] used=new boolean[200];
make old[] and new[]
for(i=0;i<200;i++){
    int n=random number from 0 to 199;
    while(used[n]) n=(n+1)%200;
    new[i]=old[n];
    used[n]=true;
}
return new;
踏雪无痕 2024-12-26 04:39:19

无法抗拒...

    String filename = "random200.txt";
    List<Integer> numbers = new ArrayList<Integer>();
    for (int i = 1; i < 201; i++)
    {
        numbers.add(i);
    }
    StringBuilder sb = new StringBuilder();
    while (!numbers.isEmpty())
    {
        int position = new SecureRandom().nextInt(numbers.size());
        Integer randomNumber = numbers.remove(position);
        sb.append(randomNumber + "\n");
    }
    try
    {
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF8"));
        out.append(sb.toString());
        out.close();
    }
    catch (Exception ex)
    {
        throw new RuntimeException(ex);
    }

Couldn't resist...

    String filename = "random200.txt";
    List<Integer> numbers = new ArrayList<Integer>();
    for (int i = 1; i < 201; i++)
    {
        numbers.add(i);
    }
    StringBuilder sb = new StringBuilder();
    while (!numbers.isEmpty())
    {
        int position = new SecureRandom().nextInt(numbers.size());
        Integer randomNumber = numbers.remove(position);
        sb.append(randomNumber + "\n");
    }
    try
    {
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF8"));
        out.append(sb.toString());
        out.close();
    }
    catch (Exception ex)
    {
        throw new RuntimeException(ex);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文