如何在 java 的二维数组中以倒序显示一维数组中的单元格?
how to display in descending reverse order the cells in a 1D array inside a 2d array in java?
我正在为 class 做作业,但我不知道我需要更改什么以及确切的位置。我开始更好地理解它们是如何协同工作的。然而,我已经尝试了很多我认为可能有用的变体,但它们从来没有按照我的预期进行。
前 5 段代码我已经通过跟踪和错误正确工作,但是第 6 段 code/problem 我不太明白。
我创建二维数组的初始代码是这样的:
public static void main(String[] args) {
int[][] numbers = {{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4},{5,5,5,5,5}};
//made this to go around Scanner to save time/workload
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < 5; columns++) {
}
}
我遇到问题的代码是:
System.out.println("2) Your entered values are now : ");
System.out.println( );
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < rows + 1; columns++) {
System.out.print(numbers [rows][columns]);
} // will replace w/ System.out.print("*"); to get asterisks instead of numbers
System.out.println( );
}
/*needs to Output ***** Outputs 1
**** 22
*** 333
** 4444
* 55555 */
我已将代码中的所有 6 个问题分开,它们都 运行 来自相同的输入 (int numbers[5][5]) 并使用类似的代码。它们都依次显示在一个页面上。
你需要你的行数从 4 到 0 而不是 0 到 4。欢迎在此处发表评论,如果需要,我会提供额外的帮助。
@Maybe_Factor
弄清楚那部分很容易。我们在最初发布一个小时后就弄清楚了问题所在。
这是新代码:
System.out.println("6) Your entered numbers are now : ");
System.out.println( );
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < 5- rows ; columns++) {
System.out.print("*");
您使用 for 循环打印二维数组的方式存在问题
你做错了
您可以通过在内部循环中将此条件 columns < rows + 1
更改为 columns < 5 - rows
来修复它:
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < 5 - rows; columns++) {
System.out.print(numbers [rows][columns]);
} // will replace w/ System.out.print("*"); to get asterisks instead of numbers
System.out.println( );
}
我正在为 class 做作业,但我不知道我需要更改什么以及确切的位置。我开始更好地理解它们是如何协同工作的。然而,我已经尝试了很多我认为可能有用的变体,但它们从来没有按照我的预期进行。
前 5 段代码我已经通过跟踪和错误正确工作,但是第 6 段 code/problem 我不太明白。
我创建二维数组的初始代码是这样的:
public static void main(String[] args) {
int[][] numbers = {{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4},{5,5,5,5,5}};
//made this to go around Scanner to save time/workload
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < 5; columns++) {
}
}
我遇到问题的代码是:
System.out.println("2) Your entered values are now : ");
System.out.println( );
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < rows + 1; columns++) {
System.out.print(numbers [rows][columns]);
} // will replace w/ System.out.print("*"); to get asterisks instead of numbers
System.out.println( );
}
/*needs to Output ***** Outputs 1
**** 22
*** 333
** 4444
* 55555 */
我已将代码中的所有 6 个问题分开,它们都 运行 来自相同的输入 (int numbers[5][5]) 并使用类似的代码。它们都依次显示在一个页面上。
你需要你的行数从 4 到 0 而不是 0 到 4。欢迎在此处发表评论,如果需要,我会提供额外的帮助。
@Maybe_Factor
弄清楚那部分很容易。我们在最初发布一个小时后就弄清楚了问题所在。
这是新代码:
System.out.println("6) Your entered numbers are now : ");
System.out.println( );
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < 5- rows ; columns++) {
System.out.print("*");
您使用 for 循环打印二维数组的方式存在问题
你做错了
您可以通过在内部循环中将此条件 columns < rows + 1
更改为 columns < 5 - rows
来修复它:
for(int rows = 0; rows < 5; rows++) {
for(int columns = 0; columns < 5 - rows; columns++) {
System.out.print(numbers [rows][columns]);
} // will replace w/ System.out.print("*"); to get asterisks instead of numbers
System.out.println( );
}