0基础学java_逻辑变量 逻辑表达式 条件句

Coding is something that can be learned

                                                                        --Chris Bosh

1.逻辑变量   boolean

true/false

 

2.逻辑表达式

例如:(X > y)& (y != z)

package com.feimao.code;

public class Boolea {
    public static void main(String args[]){
        boolean logic;
        logic = true;
        int x = 4 , y = 2 , z = 8 , w = 3;
        logic = (x - 1 >= y) & (!((z < w) | (y + 6 != z)));
        System.out.println(logic);
    }
}

 

3.条件句

if(logic expression){

statement……

}

package com.feimao.code;



public class If {

    public static void main(String args[]) {

        int haogan = 90;

        if (haogan >= 90) {

            System.out.println("啪啪啪");

        }

        if (haogan > 60 && haogan < 90) {

            System.out.println("你是一个好人");

        }

        if (haogan <= 60) {

            System.out.println("呵呵,去洗澡了");

        }

    }

}

&& 和&的区别?

 

 

if-else

if(条件){

条件为true的时候执行的语句

}

else{

条件为false的时候执行的语句

}

package com.feimao.code;



public class IfElse {

    public static void main(String args[]){

        int x = 5 , y = 2 , z = 3;

        if(x > y){

            if(x > z){

                System.out.println(x);

            }

            else{

                System.out.println(z);

            }

        }

        else{

            if(y > z){

                System.out.println(y);

            }

            else{

                System.out.println(z);

            }

        }

    }

}

 

  1. 使逻辑结构更清晰明确,一环套一环
  2. 提高判断效率

if--else-if

if(条件一){

代码一;

}

else if(条件二){

代码二;

}

else if(条件三){

代码三;

}

else if(条件四){

代码四;

}

else{

   代码五;

}

package com.feimao.code;



public class BaoZi {

    public static void main(String args[]) {

        double price = 1.5;

        int quantity;

        if (price > 3) {

            quantity = 0;

        } else if (price > 2) {

            quantity = 3;

        } else if (price > 1) {

            quantity = 5;

        } else if (price > 0.5) {

            quantity = 8;

        } else {

            quantity = 10;

        }

        System.out.println(quantity);

    }

}

 

 

一般情况下,if—else-if结构中,根据条件判断,只会有一个大括号中的代码被执行,非常适合用来做分类讨论

 

 

 

猜你喜欢

转载自www.cnblogs.com/feimaoyuzhubaobao/p/9704487.html
今日推荐