基质填充块[英] Matrix filler block

本文是小编为大家收集整理的关于基质填充块的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的班级中,我们必须制作一个矩阵填充程序,但是我对如何使用用户输入对此感到非常困惑,而且我根本不知道该怎么做. iv'e试图开始编码,但无法超越步骤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");
        }
    }
}

所以我修复了代码,并且正在徘徊,我该如何添加数字(如1和2)的零地狱,以便它出现01和02,我是否需要一个if循环,以便它仅检查数字少于10?<<<<<<<<<<

推荐答案

通过您的示例代码,您所缺少的是基本语法知识.让我用简单的语言在最基本的层面上刷新您的内存.

数组就像某种类型的变量的多维列表.

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

这是一些代码:

int[] myArray;

在这里i 声明一个1维数组,可容纳ints.

myArray = new int[6];

在这里i 初始化我的数组,并将维度的长度设置为6.

int[] myArray2 = new int[7];

我也可以在同一行上进行.

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

在这里,我声明了一个2维数组,该阵列容纳long s.尺寸的长度为3和2,因此当您想象它时看起来像这样:

_ _
_ _
_ _

现在,我们将 Access 在某个位置处的数组.这是通过指定数组名称和要访问的每个维度中的位置来完成的,例如:

myMatrix[0][1] = 63;

记住!位置从0开始计数,因此2 x 3数组将具有第一个维度值0和1;第二维值0、1和2.

现在让我们迭代在一个数组上,并将数字6放在其所有插槽中:

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++) {
       // ...
    }
    

本文地址:https://www.itbaoku.cn/post/978353.html

问题描述

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++) {
       // ...
    }