day03
大纲:
运算符
分支结构
1.运算符:
1)算术运算符:+-*/%,++,--
2)关系运算符:>,<,>=,<=,==,!= boolean
3)逻辑运算符:&&,||,! boolean
4)赋值运算符:=,+=,-=,*=,/=,%=
5)字符串连接运算符:+
6)三目/条件运算符:boolean?数1:数2
2.分支结构:
1)if结构:1条路
2)if...else结构:2条路
3)if...else if结构:多条路
4)switch...case结构:多条路
优点:效率高、结构清晰
缺点:整数、相等
break:跳出switch
任何复杂的程序逻辑都可以通过三种结构来实现:
1)顺序结构:从上往下逐行执行,每句必走
2)分支结构:有条件的执行,并非每句必走
3)循环结构:
闰年的判断公式:
1.能被4整除并且不能被100整除
或者
2.能被400整除
例:(year%4==0 && year%100!=0) || year%400==0
个人整理代码示例:
package day01; //三目运算 public class Test01 { public static void main(String[] args) { int a = -3; String r = a>0?"正数":(a == 0?"0":"负数"); System.out.println(r);//结果为负数 } }
课上代码示例:
package day03; //运算符的演示 public class OperDemo { public static void main(String[] args) { /* * 6.三目/条件运算符: * 1)语法: * boolean?数1:数2 * 2)执行过程: * 计算boolean的值: * 若为true,则整个表达式的结果为数1 * 若为false,则整个表达式的结果为数2 */ /* int a=55,b=8; int max = a>b?a:b; System.out.println("max="+max); */ /* * 5.字符串连接运算符: * 1)+: * 1.1)若两边都是数字,则做加法运算 * 1.2)若两边出现字符串,则做字符串连接 */ /* System.out.println(100+200+""+300); //"300300" System.out.println(100+200+300+""); //"600" System.out.println(""+100+200+300); //"100200300" int age = 37; System.out.println("age="); //age= System.out.println(age); //37 System.out.println("age="+age); System.out.println("我的年龄是:"+age); System.out.println("我今年"+age+"岁了"); */ /* * 4.赋值运算符: * 1)简单赋值运算符:= * 2)扩展赋值运算符:+=,-=,*=,/=,%= */ /* int a = 5; a += 10; //相当于a=a+10 System.out.println(a); //15 a *= 2; //相当于a=a*2 System.out.println(a); //30 a /= 6; //相当于a=a/6 System.out.println(a); //5 */ /* * 3.逻辑运算符: * 1)&&:短路与(并且),两边都为真则为真,见false则false * 若第1个数为false则发生短路(后面的不执行了) * ||:短路或(或者),有一边为真则为真,见true则true * 若第1个数为true则发生短路(后面的不执行了) * !:逻辑非(取反),非真则假,非假则真 * 2)逻辑运算是建立在关系运算的基础之上的, * 逻辑运算的结果也是boolean型 */ /* int a=5,b=10,c=5; boolean b5 = a<b || c++>2; System.out.println(b5); //true System.out.println(c); //5,发生短路了 boolean b4 = a>b && c++>2; System.out.println(b4); //false System.out.println(c); //5,发生短路了 */ /* boolean b3 = !(a>b); System.out.println(b3); //!false=true System.out.println(!(a<b)); //!true=false System.out.println(a>=b || c<b); //false||true=true System.out.println(c<=b || a>b); //true||false=true System.out.println(a!=b || b>c); //true||true=true System.out.println(b==c || c>b); //false||false=false boolean b2 = a>=b && c<b; System.out.println(b2); //false&&true=false System.out.println(c<=b && a>b); //true&&false=false System.out.println(b==c && c>b); //false&&false=false System.out.println(a!=b && b>c); //true&&true=true */ /* * 2.关系运算符: * 1)>(大于),<(小于) * >=(大于或等于),<=(小于或等于) * ==(等于),!=(不等于) * 2)关系运算的结果为boolean型, * 关系成立则为true,关系不成立则为false */ /* int a=5,b=10,c=5; boolean b1 = a>b; System.out.println(b1); //false System.out.println(c<b); //true System.out.println(b>=a); //true System.out.println(c<=a); //true System.out.println(a==c); //true System.out.println(a!=c); //false */ /* * 1.算术运算符:+,-,*,/,%,++,-- * 1)%:取模/取余,余数为0即为整除 * 2)++/--:自增1/自减1,可在变量前也可在变量后 * 2.1)单独使用时,在前在后无差别 * 2.2)被使用时,在前在后有差别: * a++的值为a * ++a的值为a+1 */ /* int a=5,b=5; //1)输出a--的值5 2)a自减1变为4 System.out.println(a--); //5 System.out.println(a); //4 //1)输出--b的值4 2)b自减1变为4 System.out.println(--b); //4 System.out.println(b); //4 */ /* int a=5,b=5; int c=a++; //1)将a++的值5赋值给c 2)a自增1变为6 int d=++b; //2)将++b的值6赋值给d 2)b自增1变为6 System.out.println(a); //6 System.out.println(b); //6 System.out.println(c); //5 System.out.println(d); //6 */ /* int a=5,b=5; a++; //相当于a=a+1 ++b; //相当于b=b+1 System.out.println(a); //6 System.out.println(b); //6 */ /* System.out.println(5%2); //1,商2余1 System.out.println(6%2); //0,商3余0--整除 System.out.println(2%6); //2,商0余2 System.out.println(8.675%2); //0.675 */ } }
package day03; //分支结构的练习 public class IfDemo { public static void main(String[] args) { int num = 4; switch(num){ //byte,short,char,int case 1: //if(num==1) System.out.println(111); break; case 2: //以此为入口 System.out.println(222); break; //跳出switch case 3: System.out.println(333); break; default: //所有case都未匹配时执行 System.out.println(666); } package day03; import java.util.Scanner; //闰年的判断 public class LeapYear { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入年份:"); int year = scan.nextInt(); boolean flag = (year%4==0 && year%100!=0) || year%400==0; String msg = flag ? year+"是闰年" : year+"不是闰年"; System.out.println(msg); } } package day03; import java.util.Scanner; //成绩等级判断 public class ScoreLevel { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scan.nextInt(); if(score<0 || score>100){ System.out.println("成绩不合法"); }else if(score>=90){ //score>=0 && score<=100 System.out.println("A-优秀"); }else if(score>=80){ System.out.println("B-良好"); }else if(score>=60){ System.out.println("C-中等"); }else{ System.out.println("D-不及格"); } } } package day03; import java.util.Scanner; //年龄判断程序 public class AgeRange { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入年龄:"); int age = scan.nextInt(); System.out.println(age>=18 && age<=50); } } package day03; import java.util.Scanner; //柜台收银程序 public class Cashier { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入单价:"); double unitPrice = scan.nextDouble(); System.out.println("请输入数量:"); double amount = scan.nextDouble(); System.out.println("请输入金额:"); double money = scan.nextDouble(); //计算总价(单价*数量) double totalPrice = unitPrice*amount; if(totalPrice>=500){ //满500 totalPrice*=0.8; //打8折 } if(money>=totalPrice){ //够 double change = money-totalPrice; //计算找零(给的钱-总价) System.out.println("总价为:"+totalPrice+",找零为:"+change); }else{ //不够 System.out.println("ERROR!钱不够"); } } } package day03; import java.util.Scanner; //命令解析程序 public class CommandBySwitch { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请选择功能: 1.显示全部记录 2.查询登录记录 0.退出"); int command = scan.nextInt(); switch(command){ case 1: System.out.println("显示全部记录"); break; case 2: System.out.println("查询登录记录"); break; case 0: System.out.println("欢迎下次再来"); break; default: System.out.println("输入错误"); } } }