学习笔记:https://www.bilibili.com/video/BV18J411W7cE?p=1
关系运算符:
== != > >= < <=
返回值为布尔类型的值 true/false
逻辑运算符:& | ^ !
短路逻辑运算符:&& ||
与基本逻辑运算符的区别:
&&若左边条件为false,不执行后面的部分
|| 若左边条件为true,则不执行后面的部分
public class demo{
public static void main(String[] args){
int i = 10;
int j = 20;
System.out.println((i++ > 100)&&(j++ > 100)); //j++未执行
//System.out.println((i++ > 100)&(j++ > 100));
System.out.println(i);
System.out.println(j);
}
}
三元运算符:
关系表达式 ? 表达式1 : 表达式2;
eg: a > b ? a : b;
public class demo{
public static void main(String[] args){
int a = 10 ;
int b = 20 ;
int x = a > b ? a : b;
System.out.println(x);
}
}
案例:两只老虎
需求:动物园有两只老虎,已知两只老虎的体重分别为180kg、200kg,请用程序实现判断两只老虎的体重是否相同?
public class demo{
public static void main(String[] args){
int weight1 = 180;
int weight2 = 200;
boolean b = weight1 == weight2 ? true : false;
System.out.println(b);
}
}
数据输入:
Scanner使用的基本步骤:
1、导入包:
import java.util.Scanner; //必须在类定义的上面
2、创建对象:
Scanner sc = new Scanner (System.in); //sc为变量名可变,其他部分不可变
3、接收数据:
int i = sc.nextInt() ; //变量名可变,其他部分不可变
案例:三个和尚
输入三个和尚的身高,输出身高最高的身高。
import java.util.Scanner; //导入包
public class demo{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); // 新建对象
System.out.println("请输入三个和尚的身高");
int height1 = sc.nextInt(); //键盘输入
int height2 = sc.nextInt();
int height3 = sc.nextInt();
int tempheight = height1 > height2 ? height1 : height2 ;
int maxheight = height3 > tempheight ? height3 : tempheight ;
System.out.println("三个和尚中身高最高的是"+ maxheight +"cm");
}
}