矩阵填充块 [英] Matrix Filler Block

查看:93
本文介绍了矩阵填充块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的课堂上,我们必须制作一个矩阵填充程序,但是我对如何使用用户输入非常困惑,我根本不知道如何操作。我试图开始编码,但无法通过第1步。

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");
    }
}

}

所以我修复了代码并且在徘徊我怎么能添加数字的零地狱,如1和2所以它出来01和02,我需要一个if循环,以便它只检查小于10的数字?

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.


  • 声明数组时,您可以选择变量的类型。

  • 数组可以容纳的变量是一个常数(数组的 length ),它是在初始化时定义的。

  • 一个数组也可以有多个维度。您在声明数组时设置维数。将1维数组视为列表,2维将列表转换为矩阵。在这种情况下,您需要设置每个维度的长度(初始化时)。因此,如果2个维度的长度相同,则得到一个正方形,否则得到一个矩形。

  • 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;

这里我声明一个1维数组,其中包含 int s。

Here I declared a 1 dimensional array which holds ints.

myArray = new int[6];

这里我初始化我的数组并将维度的长度设置为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];

这里我宣布了一个二维数组,其中包含 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;

记住!位置从0开始计数,所以2乘3数组的第一个维度值为0和1;第二个维度值为0,1和2.

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.

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

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.
    

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

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

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


  • 这篇关于矩阵填充块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆