为什么我的变量在Java中没有被解析?

huangapple go评论79阅读模式
英文:

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(&quot;begin&quot;)) {
    long startTime;
    startTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase(&quot;stop&quot;)) {
    long endTime;
    endTime = (System.currentTimeMillis() / 1000);
} else if (args[1].equalsIgnoreCase(&quot;timer&quot;)) {
    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(&quot;begin&quot;)) {
				startTime = (System.currentTimeMillis() / 1000);
			} else if (args[1].equalsIgnoreCase(&quot;stop&quot;)) {
				endTime = (System.currentTimeMillis() / 1000);
			} else if (args[1].equalsIgnoreCase(&quot;timer&quot;)) {
				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(&quot;begin&quot;)) {
            long startTime=0;
            startTime = (System.currentTimeMillis() / 1000);
        } else if (args[1].equalsIgnoreCase(&quot;stop&quot;)) {
            long endTime=0;
            endTime = (System.currentTimeMillis() / 1000);
            
        } else if (args[1].equalsIgnoreCase(&quot;timer&quot;)) {
            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(&quot;begin&quot;)) {
            startTime = (System.currentTimeMillis() / 1000);
        } else if (args[1].equalsIgnoreCase(&quot;stop&quot;)) {
            endTime = (System.currentTimeMillis() / 1000);
            
        } else if (args[1].equalsIgnoreCase(&quot;timer&quot;)) {
            
            currentTime = (System.currentTimeMillis() / 1000);
            currentTimeForm = currentTime - startTime;
        }

huangapple
  • 本文由 发表于 2020年10月5日 00:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64197118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定