逻辑运算注意事项以及三元运算符

逻辑运算

package com.dang.www;

import java.util.Scanner;

public class Day5 {
	public static void main(String[] args) {
    //逻辑运算符
    //与(and)   或(or)   非(取反)
    boolean a=true;
    boolean b=false;
    System.out.println("a&&b="+(a&&b));//与运算;同真才为真
    System.out.println("a||b="+(a||b));//或运算,同假才假
    System.out.println("!(a&&b)="+!(a&&b));//非运算;真假互换

    //短路运算(前面是假直接返回,不看后面)
    int c=5;
    boolean d=(c<4)&&(c++<4);//c<4为假后面c++<4就不在执行
    System.out.println(d);
    System.out.println(c);


    //位运算效率高


    //字符串连接符    +,  String
    int x=10;
    int y=20;
    System.out.println(""+x+y);
    System.out.println(x+y+"");


    //三元运算符x?y:z
    //如果x==true,则结果为y,否则结果z
    int score=80;
    String type=score<60?"不及格":"及格";
    System.out.println(type);


}

}

posted @ 2022-10-12 09:34  当尖草北平铲  阅读(46)  评论(0)    收藏  举报