程序执行
顺序执行:依次执行每行代码
分支结构:好像 人碰到分岔口
循环结构:重复执行某段代码
分支结构
1.1. if分支
1,if单独使用
public static void method1() { //条件表达式 结果是 true或false int op1 = 12; int op2 = 2; int op3 = 22, op4 = 19; /** * if(布尔值){ * if分支代码块 * } * */ //如果 条件表达式结果为true 执行if分支;否则就不执行 if (op1 > op2 && op3 > op4) { System.out.println("===1"); System.out.println("===2"); System.out.println("===3"); } System.out.println("end"); }
练习
/** * 小明语文成绩大于90分,而且历史成绩大于80分,老师奖励他; * 或者 语文成绩等于100分,而且历史成绩大于70分,老师也可以奖励他 */ public static void exercise2() { int yuScore = 100; int liScore = 99; if ((yuScore > 90 && liScore > 80) || (yuScore == 100 && liScore > 70)) { System.out.println("奖励"); } /* if (yuScore > 90 && liScore > 80) { System.out.println("奖励"); } if (yuScore == 100 && liScore > 70) { System.out.println("奖励"); }*/ } /** * 如果小明的Java考试成绩大于90分,老师就奖励他一部手机 */ public static void exercise1() { int score = 62; //crtl+alt+L 自动排版 if (score > 90) { System.out.println("奖励手机"); } }
2,if...else搭配使用
/** * 2,if...else搭配使用 */ public static void method2() { int score = 58; //如果条件表达式结果为true执行if分支 否则执行else分支;这是一个 二选一的结构 if (score >= 60) { System.out.println("及格"); } else { System.out.println("不及格"); } } public static void exercise3() { int score = 99; if (score > 90) { System.out.println("奖励五年高考三年模拟"); } else { System.out.println("站着听课"); } }
3,多重if结构
/** * 多重if结构 :常用里做区间判断 * 对学员的结业考试成绩评测 * 成绩>=90 :优秀 * 成绩>=80 :良好 * 成绩>=60 :中等 * 成绩<60 :差 */ public static void method3() { int score = 39; // 最多执行一个的 当最后有else结构的是绝对会执行其中一个 if (score >= 90) { System.out.println("优秀"); } else if (score >= 80) { System.out.println("良好"); } else if (score >= 60) { System.out.println("中等"); } else { //else分支是保底的 当上面所有的分支都不满足的时候执行else分支 System.out.println("差"); } System.out.println("end"); }
注意点
public static void method5() { int score = 99; if (score < 60) { System.out.println("差"); } else if (score < 80) { System.out.println("中等"); } else if (score < 90) { System.out.println("良好"); } else { System.out.println("优秀"); } } // [0,100] 区间范围判断 public static void method4() { int score = 99; // [90,100] //如果使用的是大于号 最大的值在上面 依次递减 //如果使用的是小于号 最小的值在上面 依次递增 if (score >= 90) { System.out.println("优秀"); //[80,90) } else if (score >= 80) { System.out.println("良好"); // [60,80) } else if (score >= 60) { System.out.println("中等"); } else { System.out.println("差"); } System.out.println("end"); }
练习
/** * 输入李雷的考试成绩,显示所获奖励 * 成绩==100分,爸爸给他买辆车 * 成绩>=90分,妈妈给他买电脑 * 90分>成绩>=60分,妈妈给他买本参考书 * 成绩<60分,什么都不买 */ public static void exercise4() { int score = 98; if (score >= 100) { System.out.println("买车"); } else if (score >= 90) { System.out.println("买电脑"); } else if (score >= 60) { System.out.println("买参考书"); } else { System.out.println("什么都不买"); } }
4,工具类 Scanner
private static void exercise6() { Scanner sc = new Scanner(System.in); System.out.println("请录入学生成绩: "); int score = sc.nextInt(); if (score >= 100) { System.out.println("买车"); } else if (score >= 90) { System.out.println("买电脑"); } else if (score >= 60) { System.out.println("买参考书"); } else { System.out.println("什么都不买"); } } // Scanner工具类 接收用户的录入值 用于和用户的交互 public static void method6() { Scanner sc = new Scanner(System.in); System.out.println("请录入一个int值:"); //nextInt() 阻塞式方法 直到录入;接受一个int值 只能接收数字字符 int score = sc.nextInt(); System.out.println("请录入一个学生姓名:"); // next()接收字符串 String name = sc.next(); System.out.println("score: " + score); System.out.println("name: " + name); }
1.2. switch分支
1,基本使用
/** * switch专门用来做等值判断 */ public static void method2() { Scanner sc = new Scanner(System.in); System.out.println("请录入排名"); int rank = sc.nextInt(); switch (rank) { case 1: System.out.println("1000"); break; case 2: System.out.println("500"); //break一旦执行 终止整个switch分支 break; case 3: System.out.println("100"); break; //当上面所有的case都不满足的时候 进入default 类似于else default: System.out.println("-300"); break; } System.out.println("end"); }
2,注意点:数据类型
-
switch(表达式) 表达式的值的类型 只能是char byte short int(他们的包装类型),String,enum
public static void method4() { String rank = "第一名"; switch (rank) { case "第一名": System.out.println("1000"); break; case "第二名": System.out.println("500"); break; case "第三名": System.out.println("100"); break; default: System.out.println("-300"); break; } } public static void method3() { char rank = '1'; //switch(表达式) 表达式的值的类型 只能是char byte short int(他们的包装类型),String,enum switch (rank) { case '1': System.out.println("1000"); break; case '2': System.out.println("500"); break; case '3': System.out.println("100"); break; default: System.out.println("-300"); break; } }
3,break关键字
-
break一旦执行 终止整个switch分支
-
break如果不写会发生case穿透 多个case都会执行 只要找到一个入口进来一路向下执行直到碰到break
//break一旦执行 终止整个switch分支 //break如果不写会发生case穿透 多个case都会执行 只要找到一个入口进来一路向下执行直到碰到break public static void method5() { String rank = "第五名"; switch (rank) { case "第一名": System.out.println("1000"); break; case "第二名": System.out.println("500"); break; case "第三名": System.out.println("100"); break; // default 可以不写 一般都要写;当所有的case都不满足 进入default default: System.out.println("-300"); break; } }
4,练习
/** * 自己给定一个月份,判断属于哪个季节。 * (3-5月为春季、6-8月为夏季、9-11月为秋季、12,1,2月为冬季) */ public static void exercise2() { Scanner sc = new Scanner(System.in); System.out.println("请录入月份:"); int month = sc.nextInt(); switch (month) { case 3: case 4: case 5: System.out.println("春季"); break; case 6: case 7: case 8: System.out.println("夏季"); break; case 9: case 10: case 11: System.out.println("秋季"); break; case 12: case 1: case 2: System.out.println("冬季"); break; default: System.out.println("请录入 1- 12的值"); break; } } /** * 韩梅梅为她的手机设定了自动拨号 * 按1:拨爸爸的号 * 按2:拨妈妈的号 * 按3:拨爷爷的号 * 按4:拨奶奶的号 */ public static void exercise1() { Scanner sc = new Scanner(System.in); System.out.println("请录入号码 "); int phoneNum = sc.nextInt(); switch (phoneNum) { case 1: System.out.println("爸爸的号"); break; case 2: System.out.println("妈妈的号"); break; case 3: System.out.println("爷爷的号"); break; case 4: System.out.println("奶奶等号"); break; default: System.out.println("请录入 1-4 的值"); break; } }