为什么此代码在 Coderbyte 编辑器中编译,而不是在我自己的 IDE 中编译?



我指的是这个扫描仪问题程序 https://coderbyte.com/editor/Find%20Intersection:Java

那里:

public static String FindIntersection(String[] strArr) {
return strArr[0];
}
public static void main (String[] args) {  
// keep this function call here     
Scanner s = new Scanner(System.in);
System.out.print(FindIntersection(s.nextLine())); 
}

s.nextLine()怎么能等于FindIntersection(String[] strArr)你不能转换为扫描仪读数线到数组,我们可以吗!

此代码在我的 java IDE 中抛出数组

该代码无法编译。

似乎 Coderbyte 只是在编译之前用输入替换s.nextLine()。当您将输入更改为无法编译的内容时,您可以在错误输出中看到以下内容:

Main.java:14: error: cannot find symbol
System.out.print(FindIntersection(new tring[] {"1, 3, 4, 7, 13", "1, 2, 4, 13, 15"})); 
^
symbol:   class tring
location: class Main

如果您将s重命名为scanner它将无法替换它,并且您将收到您应该收到的错误消息:

Main.java:14: error: incompatible types: String cannot be converted to String[]
System.out.print(FindIntersection(scanner.nextLine())); 
^

我不确定他们为什么选择这样做。这是非常具有误导性的。

最新更新