Java中布尔值和if-else的一些理解

/**
 * 布尔运算和if-else条件语句的一些理解
 * @author Administrator
 *
 */
public class BooleanTest {
    
    public static void main(String[] args){
        boolean a = true;
        boolean b = false;
        boolean c = true;
        
        if(a == true)
        if(b == true)
        if(c == true)
            System.out.println("Some things are true in this world");
        //else要与最近的if配对
        else
            System.out.println("Nothing is true in this world");
        //else也要与最近的if配对,如果它的前面有else,就再往外一层配对if
        else if(a&&(b=c))
            System.out.println("It's too confusing to tell what is true");//这是为最后输出的语句
        else
            System.out.println("Hey this won't compile");
        else
            System.out.println("HaHa! I can come here");
        
        
        System.out.println(b=c);//赋值运算,c的地址赋给b,b的值变为true,所以输出true
        System.out.println(b!=c);//逻辑运算,b的值与c的值作不等于比较,true != true 所以输出为false
        System.out.println(c=b);//赋值运算,b的地址赋给c,此时b已经为true,所以输出true
        System.out.println(c!=b);//逻辑运算,true!=true 输出为false
        System.out.println(b);//b为true,输出true
        System.out.println(c);//c为true,输出true
        
        //如果要写布尔值比较的话,最好写成true==c,可以避免少写等号的错误
        if(true==c){
             // ……
        }
            
        //如果写成
        if(c=true){
            // ……
        }
        //无论c是true还是false都会进入循环
        
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值