[英]How to attach randomly generated numbers from a method into an 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");
}
}
}
}
我試過網上的很多例子,但它們總是打印指定的數組,而不是通過另一種方法隨機生成的數組。 所以我遇到的問題是將“generateSample”中創建的數字鏈接到數組本身,這樣我就可以像這樣成功地打印它們;
4 3 6
1 6 8
4 7 9
但是由於我沒有成功地在我的添加和打印方法之間鏈接我的數組,所以它是空白的。
這是我發布的最后一份報告以尋求幫助,但我們將不勝感激任何幫助。 謝謝。
您的generateSample
方法需要實際將值寫入數組。 然后,只有在整個二維數組填滿后,才應該打印它:
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);
}
我已經嘗試了幾個小時來讓我的 class 正常工作。 我試圖讓一個多維數組填充由我的“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");
}
}
}
}
我已經嘗試在 Internet 上遵循許多示例,但它們始終用於打印指定的數組,而不是由另一種方法隨機生成的數組。 所以我遇到的麻煩是將“generateSample”中創建的數字鏈接到數組本身,這樣我就可以像這樣成功地打印它們;
4 3 6
1 6 8
4 7 9
但是由於我沒有成功地將我的數組鏈接到我的添加和打印方法之間,所以它是空白的。
這是我發布的最后一份報告尋求幫助,但任何幫助將不勝感激。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.