java Long 不能取消引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44089710/
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
Long cannot be dereferenced
提问by
I did almost everything to solve the annoying issue with "Long cannot be dereferenced", but anything worked out. Thus, can anyone please, help me? The problem is when I check if program timed out in if(System.currentTimeMillis().longValue()==finish)
, the comparison is not working.
我几乎尽一切努力解决了“Long无法取消引用”的烦人问题,但一切都解决了。因此,任何人都可以请帮助我吗?问题是当我检查程序是否超时时if(System.currentTimeMillis().longValue()==finish)
,比较不起作用。
public void play()
{
long begin = System.currentTimeMillis();
long finish = begin + 10*1000;
while (found<3 && System.currentTimeMillis() < finish) {
Command command = parser.getCommand();
processCommand(command);
}
if(System.currentTimeMillis().longValue()==finish){
if(found==1){System.out.println("Time is out. You found "+found+" item.");}
else if(found>1 && found<3){System.out.println("Time is out. You found "+found+" items.");}}
else{
if(found==1){System.out.println("Thank you for playing. You found "+found+" item.");}
else if(found>1 && found<3){System.out.println("Thank you for playing. You found "+found+" items.");}
else{System.out.println("Thank you for playing. Good bye.");}
}
}
采纳答案by davidxxx
System.currentTimeMillis()
returns a primitive long
not a object Long
.
So you cannot invoke the longValue()
method or any method on it as primitive cannot be the object of method invocations.
System.currentTimeMillis()
返回一个原语long
而不是一个对象Long
。因此,您不能调用该longValue()
方法或其上的任何方法,因为原语不能是方法调用的对象。
Besides, it is useless to invoke longValue()
as System.currentTimeMillis() returns already a long value.
此外,longValue()
因为 System.currentTimeMillis() 已经返回一个 long 值,所以调用它是没有用的。
This is better :
这个更好 :
if(System.currentTimeMillis()==finish){
But in fact this condition : if(System.currentTimeMillis()==finish)
could not be true
even if System.currentTimeMillis() == finish
in the while
statement :
但事实上,这个条件: if(System.currentTimeMillis()==finish)
不能true
即使 System.currentTimeMillis() == finish
在while
语句:
while (found<3 && System.currentTimeMillis() < finish) {
Command command = parser.getCommand();
processCommand(command);
}
Because between the end of the while statement and the condition evaluation :
因为在 while 语句的结尾和条件评估之间:
if(System.currentTimeMillis() == finish)
, the time goes on elapsing.
if(System.currentTimeMillis() == finish)
,时间在流逝。
So you should rather use :
所以你应该使用:
if(System.currentTimeMillis() >= finish){