gpt4 book ai didi

java - Long 不能取消引用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:19 27 4
gpt4 key购买 nike

我几乎做了所有事情来解决“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.");}
}
}

最佳答案

System.currentTimeMillis() 返回原始 long 而不是对象 Long。因此您不能调用 longValue() 方法或它上面的任何方法,因为原语不能是方法调用的对象。

此外,调用 longValue() 是没有用的,因为 System.currentTimeMillis() 返回的已经是一个 long 值。

这样更好:

    if(System.currentTimeMillis()==finish){

但实际上这个条件:if(System.currentTimeMillis()==finish) 不可能是 true 即使 System.currentTimeMillis() ==在 while 语句中完成:

    while (found<3 && System.currentTimeMillis() < finish) {
Command command = parser.getCommand();
processCommand(command);
}

因为在 while 语句结束和条件评估之间:

if(System.currentTimeMillis() == finish),时间继续流逝。

所以你应该使用 :

 if(System.currentTimeMillis() >= finish){

关于java - Long 不能取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44089710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com