二维阵列/矩阵的长度(Length of a 2

编程入门 行业动态 更新时间:2024-10-24 16:23:04
二维阵列/矩阵的长度(Length of a 2-dimensional array / matrix)

所以我想构建一个接收两个矩阵元素的程序,并给出这两个矩阵的乘积。 到目前为止,我进行了一些实验,创建了一个矩阵并使用了“readInts” - 手动。 首先,我想确保程序实际上接收用户输入的元素,因此我想打印出第一个矩阵作为一个整体(然后更进一步,当然)。 这包含在此代码中:

int rows = readInt("Number of rows: "); int columns = readInt("Number of columns: "); int [][] m = new int[rows][columns]; int [] elements = readInts("Please type in the elements: "); for(int = 0; i < m.length; i++) { print(elements[i], 5); }

现在,我还不明白的是我如何解释“m.length”。 经过一些测试,我发现它指的是行数,所以例如当我定义3行并输入数字1,2,3,4,5时,我只收到1,2,3 ,所以该计划切断了其余部分。 我猜他实际上应该引用列数,所以当我定义m时我必须切换[rows]和[columns],但这是违反直觉的,因为你总是首先命名行数。 另外,我不知道迟早会不会出现其他问题。 那么,有没有另一种方法来做到这一点,而不是在开始时切换[rows]和[columns]?

So I would like to construct a program that receives the elements of two matrices and gives out the product of these two matrices. I experimented a little bit so far, created a matrix and used the "readInts"-manual. First, I would like to make sure that the program actually receives the elements typed in by the user, so I wanted to print out the first matrix as a whole (and then go further, of course). This is included in this code:

int rows = readInt("Number of rows: "); int columns = readInt("Number of columns: "); int [][] m = new int[rows][columns]; int [] elements = readInts("Please type in the elements: "); for(int = 0; i < m.length; i++) { print(elements[i], 5); }

Now, what I didn't understand yet is how I have to interpret "m.length". After a little bit of testing, I found out that it refers to the number of rows, so for example when I define 3 rows and type in the numbers 1, 2, 3, 4, 5, I receive only 1, 2, 3, so the program cuts off the rest. I guess he actually is supposed to refer to the number of columns, so I'd have to switch [rows] and [columns] when I define m, but that's counterintuitive since you always name the number of rows first. Plus, I don't know whether there would arise other problems with that sooner or later or not. So, is there another way to do this without switching [rows] and [columns] at the beginning?

最满意答案

因为你知道行和列你可以做2循环。

int rows = readInt("rows"); int columns =readInt("columns"); int [][] m = new int[rows][columns]; for (int i = 0; i < rows; i++) { int n[]=readInts("enter 3 numbers:"); for (int k = 0; k < columns; k++) { m[i][k] = n[k]; } }

没有必要像我一样使用扫描仪(这里工作不正常)。 如果您正确读取字符串,这应该会给您想要的结果

since you know the rows and columns you can do 2 for loops.

int rows = readInt("rows"); int columns =readInt("columns"); int [][] m = new int[rows][columns]; for (int i = 0; i < rows; i++) { int n[]=readInts("enter 3 numbers:"); for (int k = 0; k < columns; k++) { m[i][k] = n[k]; } }

There is no need to use the scanner like i did(isn't working properly here). yet this should give you the desired result if you read the strings right

更多推荐

本文发布于:2023-08-02 20:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   矩阵   长度   Length

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!