有人可以帮我完成作业吗?由于某种原因,Java无法识别我的输入
重复以下错误:
Merge.java:25: cannot find symbol
symbol : class Scanner
location: class Merge
Scanner a = new Scanner( System.in );
^
public class Merge {
public static void main(String[] args) {
int[] a = { 1, 1, 4, 5, 7 };
int[] b = { 2, 4, 6, 8 };
int[] mergedArray = merge(a, b);
for (int i = 0; i < mergedArray.length; i++) {
System.out.print(mergedArray[i] + " ");
}
}
public static int[] merge(int[] a, int[] b) {
// WRITE CODE HERE
int[] mergedArray = new int[a.length() + b.length()];
int i = 0;
int j = 0;
int k = 0;
while (i < a.length() && j < b.length()) {
if (a[i] < b[j]) {
mergedArray[k] = a[i];
i++;
}
else {
mergedArray[k] = b[j];
j++;
}
k++;
}
while (i < a.length()) {
mergedArray[k] = a[i];
i++;
k++;
}
while (j < b.length()) {
mergedArray[k] = b[j];
j++;
}
}
}
答案 0 :(得分:0)
我测试了你的代码,这就是我所做的修复它。
import java.util.Scanner;
来导入扫描程序库length()
更改为length
。return mergedArray;
方法的末尾添加merge(int[] a, int[] b)
。重新编译,然后运行它。 祝你好运。