我正在为课堂做作业,但我不知道我需要改变什么以及具体在哪里。我开始更好地理解这一切是如何协同工作的。但是,我尝试了很多我认为可能有效的变体,但它们从未达到我的预期。
前 5 段代码我已经通过跟踪和错误正常工作,但是第 6 段代码/问题我不太清楚。
我创建二维数组的初始代码是这样的:
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])并使用了类似的代码。它们都依次显示在一个页面上。
您使用 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( );
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句