Java eclipse中无法访问的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19098132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unreachable code in eclipse
提问by wormwood
What does the following mean?
以下是什么意思?
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable Code
at mycode.sample.main(sample.java:24)
I'm hoping I can find the line where the error occurred. I thought "24" is the line, but I only have 23 lines of code in my project.
我希望我能找到发生错误的那一行。我认为“24”是这一行,但我的项目中只有 23 行代码。
Here's the project code
这是项目代码
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();
}
}
I would like to understand this error message. Thanks in advance.
我想了解此错误消息。提前致谢。
采纳答案by cmd
The first message, Exception in thread "main" java.lang.Error: Unresolved compilation problem:
means your code does not compile. You need to identify the error and fix it.
Modern IDEs e.g. Eclipse, Netbeans, etc flag compile errors. They can help you to quickly identify the source.
第一条消息Exception in thread "main" java.lang.Error: Unresolved compilation problem:
表示您的代码无法编译。您需要识别错误并修复它。现代 IDE,例如 Eclipse、Netbeans 等会标记编译错误。他们可以帮助您快速确定来源。
The second error:
第二个错误:
Unreachable Code
at mycode.sample.main(sample.java:24
means that the code at line 24 will never be reached.
意味着永远不会到达第 24 行的代码。
Here is an example of unreachable code:
这是一个无法访问的代码示例:
public void doSomething() {
if (true) {
return;
}
// All code below here is considered unreachable code
doSomething()
}
回答by cmd
Try changing your constructor, from:
尝试更改您的构造函数,从:
public sample (int fir,int sec)
{
fir = first;
sec = second;
}
to:
到:
public sample (int fir,int sec)
{
first = fir;
second = sec;
}