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

     

     

     

  • 相关阅读:
    在Windows10上安装Linux子系统
    《ln--软连接》--linux命令
    【转】最详细的Log4J使用教程
    7 str字符串的内置函数
    6.1 range创建数值序列
    6 int数值类型的内置函数
    linux shell 字符串操作(长度,查找,截取,替换)详解
    awk 字符串处理函数
    Shell字符串截取
    5 python基本数据类型
  • 原文地址:https://www.cnblogs.com/feimaoyuzhubaobao/p/9704487.html
Copyright © 2011-2022 走看看