【问题标题】:Why does this code compile on Coderbyte but not locally?为什么这段代码在 Coderbyte 上编译而不是在本地编译?
【发布时间】:2019-09-26 05:04:34
【问题描述】:

我正在尝试一些关于 coderbyte 的编程挑战并在我自己的 IDE 中编程,因为它更容易,但我不知道为什么它提供的代码甚至可以编译。

import java.util.*; 
import java.io.*;

class Main {  
  public static String MaximalSquare(String[] strArr) { 

    // code goes here   
    /* Note: In Java the return type of a function and the 
       parameter types being passed are defined, so this return 
       call must match the return type of the function.
       You are free to modify the return type. */

    return strArr[0];

  } 

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

}

我明白了 线程“主”java.lang.Error: Unresolved compilation problem 中的异常:

Main类型中的方法MaximalSquare(String[])不是 适用于参数(字符串)

当我尝试在自己的计算机上编译它时,这是我所期望的,因为它试图将字符串传递给 MaximalSquare(String[] strArr),但我不知道为什么它在 coderbyte 上编译得很好

【问题讨论】:

  • MaximalSquare 在 coderbyte 中是否具有相同的签名,它们是否使用可变参数而不是显式数组?
  • 看起来Coderbyte并没有真正编译程序,而是直接调用了“参数测试”框中的参数传入的方法。有点遗憾的是,Guaranteed to Make You a Better Coder 网站上的第一个挑战有一个以大写字母开头的方法!
  • 它看起来更像是 thcoderbyte 的 Scanner 被篡改以应对该挑战:nextLine() 返回一个字符串数组(例如 [Ljava.lang.String;@74a14482),如果为给定的挑战赛跑(其他都是正常) - 这不会有助于提高你的编码技能(至少你习惯了非常奇怪的行为和错误)
  • 其实很混乱;在第一个挑战中,如果我将输入从 8 更改为 8 4 我会收到很多 compile 错误,例如:')' expected System.out.print(FirstFactorial(8 4)); - 看起来整个代码是预编译的,并且 Scanner 调用被替换为输入值(代码似乎已编译,但不是 raw 输入代码)
  • @test - 结论:使用/信任您的本地 IDE;不是编码器字节

标签: java compilation


【解决方案1】:

Coderbyte 有一些底层代码以一种适用于他们为您填充的功能设置的方式对其进行解析,而普通的 IDE 需要程序员使用它来转换/解析信息。

顺便说一句,如果您确实希望它在您的 IDE 上工作(但仍然不知道如何更改它),请使用 s.nextLine().split("\\s+") 将给定字符串从扫描仪转换为字符串数组

【讨论】:

    猜你喜欢
    • 2012-11-18
    • 2012-02-14
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 2012-07-06
    • 2013-08-08
    相关资源
    最近更新 更多