基质填充块[英] Matrix filler block

问题描述

```package question4;

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

public class MatrixFiller {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Size of Matrix: ");
Random ranGen = new Random();
int matrixSize = input.nextInt();
int a = matrixSize * matrixSize;
input.close();
int[][] myMatrix = new int[matrixSize][matrixSize];
for (int x = 0; x < matrixSize; x++) {
for (int y = 0; y < matrixSize; y++) {
myMatrix[x][y] = ranGen.nextInt(a);
System.out.print(Integer.toString(myMatrix[x][y]) + " ");
}
System.out.print("\n");
}
}
}
```

推荐答案

• 当您声明数组时，您选择变量的类型.
• 数组可以保存的变量数量是恒定数字(数组的length)，该数字是初始化..
• 时定义的
• 一个数组也可以具有多个维度.当您声明数组时，您会设置尺寸的数量.将1维数组视为列表，2维度将将列表变成矩阵.在这种情况下，您需要设置每个维度的长度(初始化时).因此，如果两个维度的长度与正方形相同，否则会得到一个矩形.

```int[] myArray;
```

```myArray = new int[6];
```

```int[] myArray2 = new int[7];
```

```long[][] myMatrix = new long[3][2];
```

```_ _
_ _
_ _
```

```myMatrix[0][1] = 63;
```

```int[][] exmaple = new int[3][3]; // 2 dim. array of ints with size 3 by 3.
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
example[x][y] = 6;
}
}
```

• 您可以直接初始化带有值的数组，然后您不需要指定尺寸的长度:

```int[][] array = new int[][] {{1 ,2}, {5, 65}}; // 2 by 2
```
• 您可以通过语法获得数组的长度

```array.length;
array[0].length;
array[1].length;
// etc.
```

这些返回int，您可以在循环时用作绑定:

```for (int i = 0; i < array.length; i++) {
// ...
}
```

问题描述

In my class we have to make a matrix filler program but I have gotten very confused on how to do so by using the user input and I don't know how to at all. Iv'e tried to start coding but can't get past step 1.

```package question4;

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

public class MatrixFiller {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Size of Matrix: ");
Random ranGen = new Random();
int matrixSize = input.nextInt();
int a = matrixSize * matrixSize;
input.close();
int[][] myMatrix = new int[matrixSize][matrixSize];
for (int x = 0; x < matrixSize; x++) {
for (int y = 0; y < matrixSize; y++) {
myMatrix[x][y] = ranGen.nextInt(a);
System.out.print(Integer.toString(myMatrix[x][y]) + " ");
}
System.out.print("\n");
}
}
}
```

so i fixed the code and was wandering how can i add the zero inferno of the number like 1 and 2 so it comes out 01 and 02, do i need an if loop so that it only checks numbers less then 10?

推荐答案

By your example code it seems that what you are missing is basic syntax knowledge. Let me refresh your memory on arrays at the most basic level with simple language.

Arrays are like a multi-dimensional list of variables of some type.

• You choose the type of variables when you declare the array.
• The amount of variables which an array can hold is a constant number (the length of the array) which is defined when is is initialized.
• An array can also have more than one dimensions. You set the number of dimensions when you declare the array. Think of a 1 dimensional array as a list, 2 dimensions would turn the list into a matrix. In this case, you need to set the length of each dimension (when you initialize). So, if the length of the 2 dimensions is the same you get a square, otherwise you get a rectangle.

Here is some code to go along with this:

```int[] myArray;
```

Here I declared a 1 dimensional array which holds ints.

```myArray = new int[6];
```

Here I initialized my array and set the length of the dimension to 6.

```int[] myArray2 = new int[7];
```

I can also do them on the same line.

```long[][] myMatrix = new long[3][2];
```

Here I declared a 2 dimensional array which holds longs. The lengths of the dimensions are 3 and 2, so it looks like this when you imagine it:

```_ _
_ _
_ _
```

Now we wan to access the array at a certain position. This is done by specifying the array name and the position in each dimension you want to access, like this:

```myMatrix[0][1] = 63;
```

Remember! The position start counting from 0, so a 2 by 3 array would have the first dimension values 0 and 1; and the second dimension values 0, 1 and 2.

Now let's iterate over an array and put the number 6 in all of its slots:

```int[][] exmaple = new int[3][3]; // 2 dim. array of ints with size 3 by 3.
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
example[x][y] = 6;
}
}
```

Not sure if you need this, but I will mention a few additional notes:

• You can initialize an array with values directly and then you don't need to specify the dimensions' lengths:

```int[][] array = new int[][] {{1 ,2}, {5, 65}}; // 2 by 2
```
• You can get the length of a dimension of an array by the syntax

```array.length;
array[0].length;
array[1].length;
// etc.
```

These return an int which you can use as a bound when looping:

```for (int i = 0; i < array.length; i++) {
// ...
}
```