2015-06-24 39 views
3

我已经查看了类似词语或意图的问题,但似乎找不到能帮助我的问题。我需要将随机生成的数字插入到数组中

我已经使用用户输入创建了一个数组,我需要生成100到10000之间的数字并将其插入到数组中。我已经创建了数组,它显示为填充零,并且生成了随机数,但我似乎无法将随机生成的数字插入到数组中。

这是我的代码到目前为止。

import java.util.*; 

public class Print2DArray { 

    public static void main(String[] args) { 

     System.out.println(); 
     Scanner userInput = new Scanner(System.in); 

     System.out.println("Enter the number of rows"); 
     int rows = userInput.nextInt(); 

     System.out.println("Enter the number of columns"); 
     int columns = userInput.nextInt(); 

     int[][] array = new int[rows][columns]; 
     for (int a = 0; a < rows; a++) 
      for (int b = 0; b < columns; b++) 
       array[a][b] = 0; 

     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < columns; j++) { 
       System.out.print(array[i][j]); 
       { 
        Random r = new Random(); 
        int rand = r.nextInt(10000 - 100) + 100; 
       } 
      } 
      System.out.println(); 
     } 
    } 
} 
+2

'阵列[i] [j] = RAND;' –

+2

你在哪里插入它???像'array [i] [j] = rand'这样的东西^ – Codebender

+0

^他们说什么。您还在打印数组元素*之前*生成随机数 – samgak

回答

4
import java.util.Random; 
import java.util.Scanner; 

public class QuickTester { 

    public static void main(String[] args) { 

     Scanner userInput = new Scanner(System.in); 

     System.out.print("Enter the number of rows: "); 
     int rows = userInput.nextInt(); 

     System.out.print("Enter the number of columns: "); 
     int columns = userInput.nextInt(); 

     // Create the Random instance once here! 
     Random rand = new Random(); 
     int[][] array = new int[rows][columns]; 
     for (int a = 0; a < rows; a++) 
      for (int b = 0; b < columns; b++) 
       array[a][b] = rand.nextInt(10000 - 100) + 100; 

     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < columns; j++) { 
       System.out.print(array[i][j] + " "); 
      } 
      System.out.println(); 
     } 
    } 
} 

输入/输出:

Enter the number of rows: 3 
Enter the number of columns: 2 
574 5286 
1550 5259 
8343 4877 

注:

  • 创建随机只有一次,而不是在for循环
  • 双的每次迭代
  • 如果你想要100到10000(含),它需要是nextInt(10000-100+1) + 100
  • Random#nextInt(int n)给你[0,9990)或[0,9989],所以你从表达式得到的最大值是9999,而不是10000
+0

谢谢,这是诀窍! – ThisIsntAUserName

+1

@ThisIsntAUserName很高兴帮助! :)请记住答案中提到的几点。 – Gosu

1
array[i][j] = r.nextInt(10000 - 100) + 100; 
+0

谢谢你的帮助! – ThisIsntAUserName

1

您没有将rand分配给数组元素。 您的第二个循环应该是

Random r = new Random(); 
for(int i = 0; i<rows; i++) 
{ 
    for(int j = 0; j<columns; j++) 
    { 

     array[i][j] = r.nextInt(900) + 100; 
     System.out.print(array[i][j] + " "); 
    } 
    System.out.println(); 
} 
+2

当你想得到一个随机整数时,创建一个新的随机对象不是一个好主意吗? – Gosu

+1

@Gosu:你说得对,但我更关注问题,而不是代码重构 –

1

你需要3件事情要做,

  • 分配随机值到数组。

  • 只能使用一个嵌套的for loop's。

  • 请勿在循环内初始化Random r

尝试这种情况:

int[][] array = new int[rows][columns]; 
Random r = new Random();  
for (int a = 0; a < rows; a++) 
    for (int b = 0; b < columns; b++) { 
     array[a][b] = r.nextInt(10000 - 100) + 100; 
     System.out.print(array[a][b]); 
    } 
    System.out.println(); 
}