感謝您閱讀我的問題:
public class Gugudan_array {
public static void main(String[] args) {
for(int i = 2; i<10; i ) {
for(int j = 1; j<10; j ) {
System.out.println(i * j);
}
System.out.println();
}
}
}
在上面的乘法表中,結果正確地來自 2-9,但是:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int h = 0; h < result.length; h ) {
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[h] = (i * j);
}
}
}
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
}
在這個帶有陣列的代碼中,結果只有 81。我做錯了什么?謝謝!
uj5u.com熱心網友回復:
我將首先解釋為什么您的代碼不起作用,然后再介紹一個正確的解決方案:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int h = 0; h < result.length; h ) {
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[h] = (i * j);
}
}
}
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
}
主要問題在于這段代碼:
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[h] = (i * j);
}
}
在這里,您不斷地覆寫 的值result[h],因此一旦回圈結束并且兩個i = 9, 和j = 9,代碼將執行result[h] = 9 * 9,然后繼續執行下一個h。
我的解決方案:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[i - 2] = (i * j);
}
}
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
}
輸出:
18
27
36
45
54
63
72
81
0
0
首先,請注意我是如何完全擺脫h回圈的。那是因為我們可以根據 來制作索引i。當我們確定第一個數字時i = 2,我們希望將該數字存盤在0陣列的第 th 個索引中。同樣,當我們得到第二個數字時i = 3,我們希望將結果存盤在1陣列的第 st 索引中。
總而言之,每當我們計算結果時,我們都希望將其存盤在i - 2陣列的第 th 索引中。
使用二維陣列的更好解決方案:
int[][] result = new int[8][9];
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[i - 2][j - 1] = (i * j);
}
}
for(int a = 0; a < result.length; a ) {
for(int b = 0; b < result[a].length; b ){
System.out.print(result[a][b] " ");
}
System.out.println();
}
輸出:
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
注意:如果您希望輸出與原始代碼匹配,請更改System.out.print(result[a][b] " ");為System.out.println(result[a][b])
將乘法表存盤在二維陣列中是最有意義的。
此代碼通過映射i * j到[i - 2][j - 1]二維陣列的第 ' 個元素來作業,因此2 * 1, 最終將在result[0][0]
我希望這是有道理的!如果您需要任何進一步的幫助或澄清,請告訴我!
uj5u.com熱心網友回復:
您必須h在所有迭代后增加變數。這是完整的代碼:
public static void main(String[] args) {
int[] result = new int[10];
for (int h = 0; h < result.length; h ) {
for (int i = 2; i < 10; i ) {
for (int j = 1; j < 10; j ) {
result[h] = (i * j);
if (h < 9) {
h ;
}
}
}
}
for (int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
uj5u.com熱心網友回復:
問題是您當前的設定有 3 個嵌套回圈,外回圈運行 10 次,中間回圈運行 8 次,每個中間回圈運行 9 次,總共 10x8x9=720次...但我懷疑您只想要 72 個結果,就像您在第一個示例中顯示的那樣,因此我們需要先洗掉外回圈for(int h = 0; h < result.length; h ) {。
現在的問題是我們需要存盤所有 72 個結果,但是您當前的陣列只適合 10 個結果int[] result = new int[10];,我們可以用更大的陣列來解決這個問題:
int[] result = new int[8 * 9]; //72 spaces
這是一個有效的解決方案,其中包含一個長陣列和一個跟蹤值存盤位置的計數器,請注意代碼注釋以獲取更多詳細資訊:
//Your loops calculate 8 * 9 results, so you need an array with 72 spaces
int[] result = new int[72];
//Use a variable to track the array location
int counter = 0;
//Youn only need two loops
for(int i = 2; i < 10; i ) {
for(int j = 1; j< 10; j ) {
//Save the result in the correct location
result[counter] = (i * j);
//Incriment the counter
counter ;
}
}
//Print the stored results
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
來自官方 Java 陣列指南的一些更重要的讀物: https ://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
更好的解決方案是使用二維陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/497333.html
