Netbeans 7.3.1 IDE在以下程序的指示行中指出“从未使用分配的值”:

public class JavaTest {
    static int f() {
        return Math.random() < 0.9 ? 0 : 1;
    }

    static int g() {
        return Math.random() < 0.2 ? 0 : 1;
    }

    public static void main(String[] args) {
        int ret;
        while ((ret = f()) == 0) {  // Unused assignment???
            ret = g();
            if (ret != 0)
                System.out.println(ret);
        }

        System.out.println(ret);
    }
}


我猜这是Netbeans中的错误,但是有人可以确认是否曾经看过?

最佳答案

编辑:
非常好,抱歉,我之前没有看到。我现在同意您的观点,并且可以在Eclipse Juno SR2中使用您的确切代码来确认没有关于未使用分配的警告。 Netbeans错误!

原版的:
Netbeans是正确的...您在该行之后立即为ret分配一个新值,因此您最好将f()与0比较,例如while(f() == 0)

关于netbeans - Netbeans 7.3错误报告了“未使用的分配”提示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19097249/

10-11 22:33
查看更多