Java Scanner輸入兩個陣列
阿新 • • 發佈:2019-02-07
題目
從命令列讀入兩個陣列的長度和陣列的值,其中第一行兩個數na和nb代表aa和bb陣列的長度
程式碼
import java.util.Scanner;
public class Z {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int na = scanner.nextInt();
int nb = scanner.nextInt();
int[] aa = new int[na];
int [] bb = new int[nb];
int i = 0;
while (scanner.hasNext()) {
aa[i++] = scanner.nextInt();
if (i == aa.length) {
break;
}
}
i = 0;
while (scanner.hasNext()) {
bb[i++] = scanner.nextInt();
if (i == bb.length) {
break;
}
}
for (int j = 0; j < aa.length; j++) {
System.out.print(aa[j] + " ");
}
System.out.println();
for (int j = 0; j < bb.length; j++) {
System.out.print(bb[j] + " ");
}
}
}
結果
輸入:
3 4
1 2 3
1 2 3 4
輸出:
1 2 3
1 2 3 4