简体   繁体   English

如何将方法中随机生成的数字附加到数组中

[英]How to attach randomly generated numbers from a method into an Array

I've been trying for some hours now to get my class to work correctly.我已经尝试了几个小时才能让我的课程正常工作。 I'm trying to have a multidimensional array filled with random numbers created by my 'generateSample' method, then print the array.我试图让一个多维数组充满由我的“generateSample”方法创建的随机数,然后打印该数组。

public class GenerateUranium
{
int tray [][] = new int [5][3];
private int grading = 0;
private Random randomGenerator;

public GenerateUranium()
{
    randomGenerator = new Random();
}

public void generateSampleUranium()
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            grading = randomGenerator.nextInt(50);
            printTray(tray);
        }
    }
}

public void printTray(int a[][])
{
   for( int row = 0 ; row < a.length ; row++) {
       for (int column = 0 ; column< a[row].length ; column++) {
           System.out.print(a[row][column]+"\t");
        }
   }
}
}    

I've tried following many examples on the Internet, but they're always for printing a specified array, not one being randomly generated by another method.我试过网上的很多例子,但它们总是打印指定的数组,而不是通过另一种方法随机生成的数组。 So the trouble I'm having is linking the numbers created in the 'generateSample' to the array itself, so that I may successfully print them like so;所以我遇到的问题是将“generateSample”中创建的数字链接到数组本身,这样我就可以像这样成功地打印它们;

4 3 6 4 3 6

1 6 8 1 6 8

4 7 9 4 7 9

But as I have not been successful in linking my array between my methods for both adding to it and printing it, it is blank.但是由于我没有成功地在我的添加和打印方法之间链接我的数组,所以它是空白的。

It's of last report I post for help, but any help would be greatly appreciated.这是我发布的最后一份报告以寻求帮助,但我们将不胜感激任何帮助。 Thanks.谢谢。

Your generateSample method needs to actually write values into the array.您的generateSample方法需要实际将值写入数组。 Then, only after the entire 2D array is full, you should print it:然后,只有在整个二维数组填满后,才应该打印它:

public void generateSample() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 3; j++) {
            this.tray[i][j] = randomGenerator.nextInt(50);
        }
    }
    printTray(tray);
}

I've been trying for some hours now to get my class to work correctly.我已经尝试了几个小时来让我的 class 正常工作。 I'm trying to have a multidimensional array filled with random numbers created by my 'generateSample' method, then print the array.我试图让一个多维数组填充由我的“generateSample”方法创建的随机数,然后打印该数组。

public class GenerateUranium
{
int tray [][] = new int [5][3];
private int grading = 0;
private Random randomGenerator;

public GenerateUranium()
{
    randomGenerator = new Random();
}

public void generateSampleUranium()
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            grading = randomGenerator.nextInt(50);
            printTray(tray);
        }
    }
}

public void printTray(int a[][])
{
   for( int row = 0 ; row < a.length ; row++) {
       for (int column = 0 ; column< a[row].length ; column++) {
           System.out.print(a[row][column]+"\t");
        }
   }
}
}    

I've tried following many examples on the Internet, but they're always for printing a specified array, not one being randomly generated by another method.我已经尝试在 Internet 上遵循许多示例,但它们始终用于打印指定的数组,而不是由另一种方法随机生成的数组。 So the trouble I'm having is linking the numbers created in the 'generateSample' to the array itself, so that I may successfully print them like so;所以我遇到的麻烦是将“generateSample”中创建的数字链接到数组本身,这样我就可以像这样成功地打印它们;

4 3 6 4 3 6

1 6 8 1 6 8

4 7 9 4 7 9

But as I have not been successful in linking my array between my methods for both adding to it and printing it, it is blank.但是由于我没有成功地将我的数组链接到我的添加和打印方法之间,所以它是空白的。

It's of last report I post for help, but any help would be greatly appreciated.这是我发布的最后一份报告寻求帮助,但任何帮助将不胜感激。 Thanks.谢谢。

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

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