在blow code內部第三個while while循環不工作請告訴我爲什麼? 在這裏,我試圖用for循環替換第三個內部while循環它正常工作,但爲什麼不用while while循環.....?你能給我真正的理由嗎?第三個內部while while循環不工作
import java.util.Scanner;
public class MergeArray {
void arrayInitialization(Scanner arg) {
//entering test cases
System.out.println("enter test cases");
int t = arg.nextInt();
int k, l, i;
k = 0;
l = 0;
i = 0;
//outer while loop
while (t-- > 0) {
//initializing a1[]'s size
System.out.println("enter a1[]'s size");
int as1 = arg.nextInt();
int a1[] = new int[as1];
//inner while loop-1
while (as1-- > 0) {
System.out.println("enter a1[]'s elements");
a1[i] = arg.nextInt();
System.out.print(a1[i]);
i++;
}
i = 0;
//initializing a2[]'s size
System.out.println("enter a2[]'s size");
int as2 = arg.nextInt();
int a2[] = new int[as2];
//inner while loop-2
while (as2-- > 0) {
System.out.println("enter a2[]'s elements");
a2[i] = arg.nextInt();
System.out.print(a2[i]);
i++;
}
System.out.println();
int a3[] = new int[a1.length + a2.length];
int size = as1 + as2;
//inner while loop-3
while (size-- > 0) {
if (k < a1.length)
a3[l] = a1[k];
if (k < a2.length)
a3[l + 1] = a2[k];
k++;
l += 2;
}
for (int j = 0; j < (a1.length + a2.length); j++) {
System.out.print(a3[j]);
}
}
}
public static void main(String[] args) {
MergeArray ma = new MergeArray();
Scanner sc = new Scanner(System. in);
ma.arrayInitialization(sc);
}
}
我試了很多,但沒有找到解決方案。這裏我使用while循環,因爲我知道while循環將快速工作,而不是循環。
你爲什麼初始化一個while循環內的陣列? – Duke
在尋找調試幫助時,請務必清楚說明您的代碼應該做什麼以及它目前應該做什麼。否則,我們需要深入研究你的代碼,以便了解你的代碼甚至應該做什麼。 – Zabuza