运算符
基本运算符
public class OperatorDemo1 { public static void main(String[] args) { int a=10; int b=2; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println(a/b); System.out.println(5/2); //结果为2在java中整数相除结果还是整数 System.out.println(5.0/2);//结果为2.5 int i=5; //不能在这里写小数,因为类型不对 int j=2; System.out.println(1.0*i/j); //结果为2.5,
System.out.println(a%b); //结果为0,
}
}



自增自减运算符




赋值运算


关系运算符

逻辑运算符
package Operator; public class OperatorDemo2 { public static void main(String[] args) { //目标:掌握逻辑运算符的使用 //需求:要求手机必须满足尺寸大于扥何羽6.95,且内存必须大于等于8 double size =9.8; int storage =16; boolean rs= size >=6.95 & storage >=8; System.out.println(rs); //需求2, double size1 =9.8; int storage1 =16; boolean rs1= size1 >=6.95 | storage1 >=8; System.out.println(rs1); //需求3, double size2 =9.8; int storage2 =16; boolean rs2= !(size2 >=6.95); System.out.println(rs2); //需求4, double size4 =9.8; int storage4 =16; boolean rs4= size1 >=6.95 ^ storage1 >=8; System.out.println(rs4); } }

还有两种常用的特殊场景


三元运算符
package Operator; public class OperatorDemo3 { public static void main(String[] args) { //目标:掌握三元运算符的基本使用 double score =58.5; String rs =score >=60? "成绩合格":"成绩不合格"; System.out.println(rs); //目标:找出两个整数中的较大 值 int a= 99; int b=69; int max = a>b ? a:b; System.out.println(max); //找出3个整数中的较大值 int i =10; int j=45; int k=34; //先找出2个整数中较大的值 int temp =1>j ? i:j; //再找出temp里面与k中哪个较大的值 int max2 =temp>k ? temp:k; System.out.println(max2); System.out.println(10>3 || 10>3 && 10<3);//true System.out.println((10>3 || 10>3) && 10<3);//fales } }

浙公网安备 33010602011771号