JavaSE-2【数据输入、流程控制语句之分支结构】
1 package day2; 2 import java.util.Scanner; 3 4 /** 5 * @Author: haifei 6 * 数据输入、流程控制语句 7 */ 8 9 public class Demo1_0519 { 10 public static void main(String[] args){ 11 // test1(); 12 // test2(); 13 test3(); 14 } 15 16 public static void test1(){ 17 /* 18 1、导包 19 2、创建Scanner对象 20 3、接收数据 21 */ 22 Scanner sc = new Scanner(System.in); 23 // System.out.print("请输入:"); 24 // int x = sc.nextInt(); 25 // System.out.println("x=" + x); 26 27 System.out.print("请输入第1个人的身高:"); 28 int height1 = sc.nextInt(); 29 System.out.print("请输入第2个人的身高:"); 30 int height2 = sc.nextInt(); 31 System.out.print("请输入第3个人的身高:"); 32 int height3 = sc.nextInt(); 33 int tempHeight = (height1 > height2) ? height1 : height2; 34 int maxHeight = (tempHeight > height3) ? tempHeight : height3; 35 System.out.println("最高身高:" + maxHeight); 36 } 37 38 public static void test2(){ 39 /* 40 顺序结构 41 分支结构(if, switch) 42 循环结构(for, while, do…while) 43 */ 44 int a = 10; 45 int b = 20; 46 47 System.out.println("begin"); 48 if(a == b){ 49 System.out.println("a==b"); 50 } 51 System.out.println("end"); 52 53 System.out.println("begin"); 54 if(a > b){ 55 System.out.println("a>b"); 56 }else { 57 System.out.println("a<=b"); 58 } 59 System.out.println("end"); 60 61 // 案例:任意给出一个整数,请用程序实现判断该整数是奇数还是偶数,并在控制台输出该整数是奇数还是偶数。 62 Scanner sc = new Scanner(System.in); 63 System.out.print("请输入一个整数:"); 64 int num = sc.nextInt(); 65 if(num % 2 == 0){ 66 System.out.println(num + "是偶数"); 67 }else { 68 System.out.println(num + "不是偶数"); 69 } 70 71 System.out.print("请输入1-7间的一个整数:"); 72 int week = sc.nextInt(); 73 if(week == 1){ 74 System.out.println("星期一"); 75 }else if(week == 2){ 76 System.out.println("星期二"); 77 }else if(week == 3){ 78 System.out.println("星期三"); 79 }else if(week == 4){ 80 System.out.println("星期四"); 81 }else if(week == 5){ 82 System.out.println("星期五"); 83 }else if(week == 6){ 84 System.out.println("星期六"); 85 }else if(week == 7){ 86 System.out.println("星期日"); 87 }else { 88 System.out.println("输入错误"); 89 } 90 91 // 案例 92 System.out.print("请输入小明的分数(0-100整数):"); 93 int score = sc.nextInt(); 94 if (score<0 || score>100){ 95 System.out.println("输入分数有误"); 96 }else if (score>90 && score<=100){ 97 System.out.println("奖励捷安特一台"); 98 }else if (score>75 && score<=90){ 99 System.out.println("奖励手办一个"); 100 }else if (score>60 && score<=75){ 101 System.out.println("奖励棒棒糖一根"); 102 }else { 103 System.out.println("不及格,一顿打"); 104 } 105 //数据测试:正确数据,边界数据,错误数据 --> 程序健壮 106 } 107 108 public static void test3(){ 109 Scanner sc = new Scanner(System.in); 110 System.out.print("请输入1-7间的一个整数:"); 111 int week = sc.nextInt(); 112 switch (week){ 113 case 1: 114 System.out.println("周一"); 115 break; 116 case 2: 117 System.out.println("周二"); 118 break; 119 case 3: 120 System.out.println("周三"); 121 break; 122 case 4: 123 System.out.println("周四"); 124 break; 125 case 5: 126 System.out.println("周五"); 127 break; 128 case 6: 129 System.out.println("周六"); 130 break; 131 case 7: 132 System.out.println("周日"); 133 break; 134 default: 135 System.out.println("输入错误"); 136 break; 137 } 138 139 // 案例 140 System.out.print("请输入月份(1-12间整数)"); 141 int month = sc.nextInt(); 142 switch (month){ // 利用case穿透 143 case 1: 144 case 2: 145 case 12: 146 System.out.println("冬季"); 147 break; 148 case 3: 149 case 4: 150 case 5: 151 System.out.println("春季"); 152 break; 153 case 6: 154 case 7: 155 case 8: 156 System.out.println("夏季"); 157 break; 158 case 9: 159 case 10: 160 case 11: 161 System.out.println("秋季"); 162 break; 163 default: 164 System.out.println("输入错误"); 165 } 166 } 167 168 }