我有我的 java 解決方案,用于在下面的 3x3 矩陣中列印出數字 1-9:
// declare and initialize a 3x3 matrix
int matrix[][] =
{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// display matrix using for loops
// outer loop for row
for(int row =-0; row <matrix.length; row ) {
// inner loop for column
for(int column=0; column <matrix[row].length; column ) {
System.out.print(matrix[row][column] " ");
}
System.out.println(); // new line
}
所以我在上面的代碼中宣告并初始化了一個 3*3 向量(陣列或矩陣)。
但我想在不宣告和初始化向量的情況下從數字 1-9 列印出 3x3 2d 陣列。
我該如何解決這個問題?
uj5u.com熱心網友回復:
除非我遺漏了一些東西,否則您可以使用回圈 from 1to9并通過測驗該數字是否可被整除3(例如模數3是0),每隔三個術語列印一個換行符。喜歡,
for (int i = 1; i < 10; i ) {
System.out.printf("%d ", i);
if (i % 3 == 0) {
System.out.println();
}
}
不需要陣列(或向量)。
或者,您可以只列印這些值。
System.out.println("1 2 3");
System.out.println("4 5 6");
System.out.println("7 8 9");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/513622.html
標籤:爪哇数组蚀柜台
