用Java创建数字矩阵

用Java创建数字矩阵,java,loops,matrix,Java,Loops,Matrix,我正试图编写一个程序,提示用户输入一个介于1和9之间的数字,并创建一个x乘x的矩阵,其中x是给定的数字。它应该产生从1到x^2的随机数来填充矩阵。我已经计算出了,如果我输入“5”,我会得到一行5个随机数字,然后四行每行只有一个数字。我错过了什么 import java.util.Scanner; import java.util.Random; public class MatrixFiller { public static void main(String[] args) {

我正试图编写一个程序,提示用户输入一个介于1和9之间的数字,并创建一个x乘x的矩阵,其中x是给定的数字。它应该产生从1到x^2的随机数来填充矩阵。我已经计算出了,如果我输入“5”,我会得到一行5个随机数字,然后四行每行只有一个数字。我错过了什么

import java.util.Scanner;
import java.util.Random;
public class MatrixFiller
{
  public static void main(String[] args)
  {
    //Getting input from the user
    Scanner input = new Scanner(System.in);
    System.out.print("Size of Matrix(a number between 1 and 9): ");
    int matrixn = input.nextInt();
    input.close();
    //max is the largest possible number that can be calculated
    //with the given number squared.
    int max = (matrixn * matrixn);
    //Counters for building the matrix
    int i = 0;
    int j = 0;
    //Will create a line with x numbers on it but then produces
    //x lines with only one number. If given 5 it produces a
    //line with 5 numbers then four more lines with one number
    //each.
        do {
          do {
            Random rand = new Random();
            int mout = rand.nextInt(max - 0);
            System.out.print(mout + " ");
            i++;
          }
          while (i < matrixn);
          System.out.println();
          j++;
        }
        while (j < matrixn);

  }
}
import java.util.Scanner;
导入java.util.Random;
公共类矩阵填充器
{
公共静态void main(字符串[]args)
{
//从用户处获取输入
扫描仪输入=新扫描仪(System.in);
系统输出打印(“矩阵大小(介于1和9之间的数字):”;
int matrixn=input.nextInt();
input.close();
//max是可以计算的最大可能数
//用给定的数字平方。
int max=(matrixn*matrixn);
//用于构建矩阵的计数器
int i=0;
int j=0;
//将创建一条上面有x个数字的线,但随后生成
//只有一个数字的x行。如果给定5,则生成一个
//行5个数字,然后再行4个数字
//每个。
做{
做{
Random rand=新的Random();
int mout=rand.nextInt(最大值-0);
系统输出打印(mout+“”);
i++;
}
而(i
您需要在循环开始时重置
i
,否则它仍然是前一行的
matrixn

do {
    i = 0;  // It won't work without this
    do {
        Random rand = new Random();
        int mout = rand.nextInt(max - 0);
        System.out.print(mout + " ");
        i++;
    } while (i < matrixn);
    System.out.println();
    j++;
} while (j < matrixn);
do{
i=0;//没有这个就不行
做{
Random rand=新的Random();
int mout=rand.nextInt(最大值-0);
系统输出打印(mout+“”);
i++;
}而(i

虽然这样做有效,但最好使用
来代替
循环。

您的内部循环只执行一次,i每次都必须从乞讨开始

  do{
     i = 0 ;
     do {
        Random rand = new Random();
        int mout = rand.nextInt(max – 0);
        System.out.print(mout +  “ “);
        i ++;
     } while(i<matrixn);
     system.our.println();
     j++;
  } while(j < matrixn);
do{
i=0;
做{
Random rand=新的Random();
int mout=rand.nextInt(最大值–0);
系统输出打印(mout+“”);
i++;

}当(i时,该键将第一个
do
循环顶部的
i
值重置为零

或者,您也可以使用
进行
循环,因为从您的角度来看,它看起来更干净:

Random rand = new Random();
for (i=0; i<matrixn; i++) {
    for (j=0; j<matrixn; j++) {
        int mout = rand.nextInt(max);
        System.out.print(mout + " ");
    }
    System.out.println();
}
Random rand=new Random();

for(i=0;我感谢你!我不认为我们讨论了重置循环。我之前在这里试图找到一种方法向你发送pm,lol,但它没有功能:/没问题。你应该尝试使用
for
循环。代码是
for(int i=0;i
i
0
开始。是否需要指定随机范围?int mout=rand.nextInt(max-min)?范围在中定义为
[0..max)
。我删除了
-0
,因为它是多余的。