3
我有2個1d數組,我試圖用JAVA將它們填充到單個二維數組中。將兩個一維數組轉換爲單個二維數組
例如:
a[] = {2,7}
b[] = {9,1}
結果應該然後是:
result[][] = {{2,9}, {7,1}}
這是我的代碼
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Test Cases:\t");
int t = sc.nextInt();
int[] a;
int[] b;
int i, j, x;
for (int k = 0; k < t; k++) {
System.out.println("Enter 1st Array Limit:\t");
int len = sc.nextInt();
System.out.println("Enter 2nd Array Limit:\t");
int len1 = sc.nextInt();
a = new int[len];
b = new int[len1];
System.out.println("Enter Sum Value");
x = sc.nextInt();
System.out.println("Enter " + len + " elements:\t");
for (i = 0; i < len; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter " + len1 + " elements:\t");
for (j = 0; j < len1; j++) {
b[j] = sc.nextInt();
}
int [][] c = new int[len][2];
for (i = 0; i < len; i++) {
for (j = 0; j < len1; j++) {
if (a[i] + b[j] == x) {
for(int l = 0; i < a.length; i++){
c[l][0] = a[i];
c[l][1] = b[j];
}
}
}
}
System.out.println(Arrays.deepToString(c));
}
}
}
這仍然會產生錯誤的輸出
我想找到Fin d與給定的總和所有對
跟着你的建議仍然沒有得到正確的輸出 –
嘗試複製並粘貼此代碼,並運行它(告訴我它輸出): \t public static void main(String [] args){ \t \t int [] a = {2,7}; \t \t int [] b = {9,1}; \t \t int [] [] c = new int [a.length] [2]; (i = 0; i
[[2,9],[7,1]] –