Java 基础分支语句之程序流程控制switch-case
格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 分支结构之二: switch - case 格式 switch (表达式){ case 常量: 执行语句 //break;可省了 case 常量 2 : 执行语句; //break; 可省了 ... dfault: 执行语句; } 说明: 根据Switch表达式中的值。依次匹配各个 case 中的常量,一旦匹配成功,则进入 case 结构中,调其执行语句 当调用完执行语句以后,则仍往下执行其他 case 里的语句,直到遇到 break 关键字或者 switch - case 结构末尾止结束; break ,可以使用在 switch - case 结构中,表示一旦执行到此关键字,就跳出 switch - case 结构 switch 结构中表达式,只能是如下六种数据类型之一 byte 、 short 、 char 、 int 、枚举类型(JDK1. 5 新增)、String类型(JDK7. 0 新增) |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class HelloWorld { public static void main(String[] args) { int number = 2 ; switch (number){ case 0 : System.out.println( "zero" ); break ; case 2 : System.out.println( "one" ); break ; //执行后会打断后续执行流程 case 3 : System.out.println( "two" ); break ; default : System.out.println( "other" ); } } } 测试 one |
示例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class HelloWorld { public static void main(String[] args) { int number = 2 ; switch (number){ case 0 : System.out.println( "zero" ); //break; case 2 : System.out.println( "one" ); //break; case 3 : System.out.println( "two" ); //break; default : System.out.println( "other" ); } } } 测试 one two other |
综合代码测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int number = 2 ; switch (number) { case 0 : System.out.println( "zero" ); break ; case 2 : System.out.println( "one" ); break ; case 3 : System.out.println( "two" ); break ; default : System.out.println( "other" ); } // 获取char 类型变量,Scanner没有相关方法,只能获取一个字符串 System.out.println( "请输入您的性别:" ); String gender = scan.next(); char genderChar = gender.charAt( 0 ); //转换成char类型的操作;获取索引为0 为真上的字符 System.out.println(genderChar); System.out.println( "请输入字符:" ); String Char = scan.next(); //char cha = Char.charAt(0); switch (Char) { case "a" : System.out.println( "A" ); break ; case "b" : System.out.println( "B" ); break ; case "c" : System.out.println( "C" ); break ; case "d" : System.out.println( "D" ); break ; case "e" : System.out.println( "E" ); break ; default : System.out.println( "other" ); } System.out.println( "请输入成绩:" ); int score = scan.nextInt(); switch (score/ 10 ) { //对10 取模 case 0 : //System.out.println("不及格:"); //break; case 2 : //System.out.println("不及格"); //break; case 3 : //System.out.println("不及格"); //break; case 4 : //System.out.println("不及格"); //break; case 5 : System.out.println( "不及格" ); break ; case 6 : //System.out.println("及格"); //break; case 7 : //System.out.println("及格"); //break; case 8 : //System.out.println("及格"); //break; case 9 : //System.out.println("及格"); //break; case 10 : System.out.println( "及格" ); break ; default : System.out.println( "输入的不正确" ); } System.out.println( "请输入你家狗狗年龄:" ); int dogage = scan.nextInt(); if (dogage < 0 ) { System.out.println( "狗狗未出生" ); } else if (dogage <= 2 ) { System.out.println(dogage * 10.2 ); } else { System.out.println((dogage- 2 )* 5 + 2 * 10.2 ); } System.out.println( "请输入月份" ); int month = scan.nextInt(); System.out.println( "请输入几号:" ); int day = scan.nextInt(); int sumdays = 0 ; switch (month){ case 12 : sumdays += 30 ; case 11 : sumdays += 31 ; case 10 : sumdays += 30 ; case 9 : sumdays += 31 ; case 8 : sumdays += 31 ; case 7 : sumdays += 30 ; case 6 : sumdays += 31 ; case 5 : sumdays += 30 ; case 4 : sumdays += 31 ; case 3 : sumdays += 28 ; case 2 : sumdays += 31 ; case 1 : sumdays += day; default : System.out.println( "是今年第" +sumdays+ "天" ); } } } 测试 one 请输入您的性别: n n 请输入字符: a A 请输入成绩: 60 及格 请输入你家狗狗年龄: 8 50.4 请输入月份 9 请输入几号: 17 是今年第 260 天 |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏