所以我寫打印出合併後序列的程序,讓假設只有兩個輸入。打印完一個序列後,會打印一個空白行。此外,該程序打印每行一個字符串的合併數組。陣列的尺寸是不超過10。以下是代碼我迄今爲止:結合2個陣列
import java.util.Scanner;
public class Demo {
public static int readInput(Scanner myScanner, String[] input) {
boolean streamEnded = false;
int index = 0;
while (!streamEnded && myScanner.hasNext()) {
String value = myScanner.nextLine();
if (value.length() == 0) {
streamEnded = true;
input[index] = value;
} else {
input[index] = value;
index++;
}
}
return index;
}
public static void printArray(String[] values, int size) {
for (int index = 0; index < size; index++) {
System.out.println(values[index]);
}
public static void main(String[] args) {
int size = 10;
String[] input1 = new String[size];
String[] input2 = new String[size];
String[] merge = new String[size*2];
Scanner aScanner = new Scanner(System.in);
readInput(aScanner, input1);
readInput (aScanner, input2);
readInput (aScanner, merge);
for (int i = 0; i < input1.length; i++) {
System.out.println(input1[i]);
}
for (int i = 0; i < input2.length; i++) {
System.out.println(input2[i]);
}
for (int i = 0; i < merge.length; i++) {
if(i % 2 == 0) {
merge[i] = input1[i/2];
System.out.println(store[i]);
} else {
merge[i] = input2[i/2];
System.out.println(store[i]);
}
}
}
}
我的輸入:
1
2
3
4
5
6
7
8
所以輸出應該是:
1
5
2
6
3
7
4
8
這是我的輸出。任何人都可以幫我解決這個問題嗎?謝謝
1
2
3
4
5
6
7
8
1
2
3
4
null
null
null
null
null
5
6
7
8
null
null
null
null
null
null
1
6
3
8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Demo.main(Demo.java:47)
null
null
null
null
null
+1最佳功課的問題永遠! – Basilevs