英文:
Why isn't my variable being resolved in Java?
问题
我在Eclipse Java中遇到了一个错误。我的代码是:
```Java
if (args[0].equalsIgnoreCase("begin")) {
long startTime;
startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("stop")) {
long endTime;
endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("timer")) {
long currentTime;
long currentTimeForm;
currentTime = (System.currentTimeMillis() / 1000);
currentTimeForm = currentTime - startTime;
}
当我保存代码时,我收到错误消息“startTime cannot be resolved to a variable”。可能是因为它被分割为单独的“if”语句,所以无法找到这个变量?我正在编写一个Bukkit Minecraft插件。
我该如何修复这个错误?
<details>
<summary>英文:</summary>
I am getting an error in Eclipse Java. My code is:
```Java
if (args[0].equalsIgnoreCase("begin")) {
long startTime;
startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("stop")) {
long endTime;
endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("timer")) {
long currentTime;
long currentTimeForm;
currentTime = (System.currentTimeMillis() / 1000);
currentTimeForm = currentTime - startTime;
}
I get the error startTime cannot be resolved to a variable
when I save it. Is it possible that because it is separated into separate if
statements, it does not get the variable? I am coding a Bukkit Minecraft plugin.
How do I fix this error?
答案1
得分: 2
在 if 级联中之外声明变量,或者作为类的成员;根据您的问题提供的上下文较少,无法更加精确。
long startTime;
long endTime;
long currentTime;
long currentTimeForm;
if (args[0].equalsIgnoreCase("begin")) {
startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("stop")) {
endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("timer")) {
currentTime = (System.currentTimeMillis() / 1000);
currentTimeForm = currentTime - startTime;
}
英文:
declare the variables outside of the if cascade, or as members of a class; there is too little context in your question to be more precise
long startTime;
long endTime;
long currentTime;
long currentTimeForm;
if (args[0].equalsIgnoreCase("begin")) {
startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("stop")) {
endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("timer")) {
currentTime = (System.currentTimeMillis() / 1000);
currentTimeForm = currentTime - startTime;
}
答案2
得分: 1
你必须像这样实例化变量:
long startTime=0;
long endTime=0;
long currentTime=0;
long currentTimeForm=0;
if (args[0].equalsIgnoreCase("begin")) {
startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("stop")) {
endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("timer")) {
currentTime = (System.currentTimeMillis() / 1000);
currentTimeForm = currentTime - startTime;
}
英文:
You must instantiate the variables like so:
if (args[0].equalsIgnoreCase("begin")) {
long startTime=0;
startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("stop")) {
long endTime=0;
endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("timer")) {
long currentTime=0;
long currentTimeForm=0;
currentTime = (System.currentTimeMillis() / 1000);
currentTimeForm = currentTime - startTime;
}
However, since you use the startTime
in every case, you can just instantiate it outside of the if statements. You also never defined endTime
in some cases, so here is the fully revised code.
long currentTime=0;
long endTime=0;
if (args[0].equalsIgnoreCase("begin")) {
startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("stop")) {
endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase("timer")) {
currentTime = (System.currentTimeMillis() / 1000);
currentTimeForm = currentTime - startTime;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论