• 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("呵呵,去洗澡了");
    
            }
    
        }
    
    }

    && 和&的区别?

     

    &无论左边的结果是否为真,都将继续运算右边的逻辑表达式;

    &&左边的值为false时,将不会继续运算其右边的逻辑表达式,结果为false;

     如:p&&q ,当p为false的时候,跳过q的运算直接得出false

    运算规则:

    对于:&  -- >  只要左右两边有一个为false,则为false;只有全部都为true的时候,结果为true

    对于:&& -- > 只要符号左边为false,则结果为false;当左边为true,同时右边也为true,则结果为true

     

    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结构中,根据条件判断,只会有一个大括号中的代码被执行,非常适合用来做分类讨论

     

     

     

  • 相关阅读:
    Linux 高性能server编程——高级I/O函数
    中国儿童移动游戏市场解读 潜力巨大有待开发
    HDU 2152 Fruit (母函数)
    Currying vs Partial Application
    我的最爱Lambda演算——开篇
    函数式编程-数据结构+算法
    高阶函数 、高阶类型
    高阶函数-哈哈
    备份-泛函编程(23)-泛函数据类型-Monad
    高阶函数
  • 原文地址:https://www.cnblogs.com/feimaoyuzhubaobao/p/9704487.html
Copyright © 2020-2023  润新知