gpt4 book ai didi

java - 二维数组将行打印为列java

转载 作者:行者123 更新时间:2023-12-01 18:35:35 25 4
gpt4 key购买 nike

我们被要求打印这个二维数组,并将列作为行

例如:第一列是20,11,27,必须打印:

20
11
27

到目前为止,这是我的代码,我什至无法正常打印列,你们中有人知道问题是什么以及是否可以帮助我找到问题的解决方案吗?

public class TwoDimensionalArrays
{
public static void main (String args[])
{
final int size1 = 2, size2 = 4, size3 = 5;
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};
int row = 0, col = 0;

for(row = 0; row <= size1; row++); //loops through rows
{
for(col = 0; col <= size2; col++); //loops through columns
{
System.out.println(numbers[row][col]);
}
System.out.print("\n"); //takes a new line before each new print
}
}
}

最佳答案

删除循环末尾的;

像这样:

for (row = 0; row <= size1; row++) //loops through rows
{
for (col = 0; col <= size2; col++) //loops through columns
{
System.out.print(numbers[row][col]+" ");
}
System.out.print("\n"); //takes a new line before each new print
}
<小时/>

输出:

20 25 34 19 33 
11 17 15 45 26
27 22 9 41 13

关于java - 二维数组将行打印为列java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22171738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com