-1

以下是什么意思?

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable Code    
at mycode.sample.main(sample.java:24) 

我希望我能找到发生错误的行。我认为“24”是行,但我的项目中只有 23 行代码。

这是项目代码

package mycode;
import java.io.*;

public class sample {
  int first;
  int second;

  public sample (int fir,int sec)
  {
    fir = first;
    sec = second;
  }

  public void add()
  {
    System.out.println(first+second);       
  }

  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int f = Integer.parseInt(reader.readLine());
    // int s =  Integer.parseInt(reader.r   eadLine());
    sample sample2 = new sample(f,100);
    sample2.add();
  } 
}   

我想了解此错误消息。提前致谢。

4

2 回答 2

5

尝试更改您的构造函数,从:

public sample (int fir,int sec)
{
    fir = first;
    sec = second;
}

到:

public sample (int fir,int sec)
{
    first = fir;
    second = sec;
}
于 2013-09-30T15:54:50.610 回答
4

第一条消息Exception in thread "main" java.lang.Error: Unresolved compilation problem:意味着您的代码无法编译。您需要识别错误并修复它。现代 IDE,例如 Eclipse、Netbeans 等标记编译错误。他们可以帮助您快速识别来源。

第二个错误:

Unreachable Code
at mycode.sample.main(sample.java:24

意味着永远不会到达第 24 行的代码。

以下是无法访问的代码示例:

public void doSomething() {
    if (true) {
        return;
    }
    // All code below here is considered unreachable code
    doSomething()
}
于 2013-09-30T15:38:16.147 回答