小编Hit*_*hal的帖子

函数的增量被+ =覆盖

在三元运算符中调用的方法递增变量并返回布尔值.当函数返回false时,将还原该值.我期望变量为1但是变为0.为什么?

public class Main {
    public int a=0;//variable whose value is to be increased in function
    boolean function(){
        a++;
        return false;
    }
    public static void main(String argv[]){
        Main m=new Main();
        m.a+=(m.function()?1:0);
        System.out.println(m.a);//expected output to be 1 but got a 0 !!!!!
    }
}
Run Code Online (Sandbox Code Playgroud)

java increment variable-assignment ternary

17
推荐指数
2
解决办法
784
查看次数

标签 统计

increment ×1

java ×1

ternary ×1

variable-assignment ×1