越早知道越好(java中switch default)java的switch语法,java流程控制——【switch】如何使用和结构案例分享结节【java程序代码】,java switch语句的用法,
switch 和 if 的比较:


案例1:
import java.util.Scanner;public class Switch01{public static void main(String[] arge){/* 案例: 1、请编写一个程序,该程序可以接收一个字符,比如:a,b,c,d,e,f,g2、a表示星期一,b表示星期二... 3、根据用户的输入显示相应的信息,要求使用switch语句完成 *///思路分析 //1、创建一个Scanner对象 接收一个字符 //2、使用switch语句来完成输出显示相应信息 Scanner myScanner = new Scanner(System.in);System.out.println("请输入一个字符(a-g):");char c1 = myScanner.next().charAt(0);//在java中,只要是有值返回,就是一个表达式 switch(c1){case a :System.out.println("今天是星期一,痛苦的:(");break;case b :System.out.println("今天是星期二,难过的:(");break;case c :System.out.println("今天是星期三,煎熬的:(");break;case d :System.out.println("今天是星期四,混日子的:)");break;case e :System.out.println("今天是星期五,期待的:)");break;case f :System.out.println("今天是星期六,开心的:)");break;case g :System.out.println("今天是星期日,不开心的:)");break;default:System.out.println("你输入的字符不正确,没有匹配的:)");}System.out.println("退出了switch,继续执行程序!!!");}}
案例2:
2、使用switch 把小写类型的 char型转为大写(键盘输入)。只转换a,b,c,d,e。其它的输出“other”。import java.util.Scanner;public class SwitchExercise01{public static void main(String[] arge){//创建Scanner对象 Scanner myScanner = new Scanner(System.in);System.out.println("请输入一个字符(a-e):");char c1 = myScanner.next().charAt(0);switch(c1){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("你输入有误~");}System.out.println("继续执行下一个程序!=====");}}
案例3:
3、对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。(注:输入的成绩不能大于100),提示成绩/60。import java.util.Scanner;public class SwitchExercise02{public static void main(String[] arge){//思路分析 1)这道题,可以使用 分支来完成,但是要求使用switch2)这里我们需要进行一个转换 编程思路:如果成绩在 [60,100],(int)(成绩/60) = 1如果成绩在 [0,60),(int)(成绩/60) = 0Scanner myScanner = new Scanner(System.in);System.out.println("请输入你的成绩:");double score = myScanner.nextInt();//使用if-else 保证输入的成绩是有效的 0-100 if (score >= 0 && score <= 100) {switch((int)(score / 60)){case 0 :System.out.println("你的成绩:不合格");break;case 1 :System.out.println("你的成绩:合格");break;}}else{System.out.println("请输入有效的成绩0-100");}}}
案例4:
4、根据用于指定月份,打印该月份所属的季节。3,4,5 春季6,7,8 夏季 9,10,11秋季 12,1,2 冬季 【使用穿透】。import java.util.Scanner;public class SwitchExercise02{public static void main(String[] arge){//思路分析 1)创建Scanner对象,接收用户输入2)使用 int month 接收月份3)使用switch 来匹配,使用穿透来完成Scanner myScanner = new Scanner(System.in);System.out.println("请输入月份:");int month = myScanner.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 1:case 2:case 12:System.out.println("这是冬季");break;default :System.out.println("你输入的月份不对(1-12)");}}}
注意事项和细节讨论
1、表达式数据类型,应和case后的常量类型一致,或者是可以自动转成可以相互比较的类型,比如输入的是字符,而常量是int
2、switch(表达式)中表达式的返回值必须是:(byte,short,int ,char,enum,String)
3、case子句中的值必须是常量,而不能是变量
4、default子句是可选的,当没有匹配的case时,执行default
5、break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有写break,程序会按顺序执行到switch结尾,除非遇到了break。文章持续更新:欢迎各位小伙伴关注小智,希望将我的不足之处给予指点,谢谢大家。喜欢Java,热衷学习的小伙伴可以关注我私信联系讨论分享。
版权申明
本文系作者 @河马 原创发布在河马博客站点。未经许可,禁止转载。
暂无评论数据