java switch-case练习 常见题型
一、使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输 出 “other”。
提示:String word = scan.next(); char c = word.charAt(0); switch(c){}
二、对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”
public class test1 { public static void main(String[] args) { /* 对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格” */ Scanner scan = new Scanner(System.in); System.out.println("请输入该同学的成绩检验是否合格:"); int score = scan.nextInt(); if(score<0 && score > 100){ System.out.println("抱歉,您输入的数据非法,请重新输入"); } int i = score / 10; switch (i){ case 1: 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; } } }
三、从键盘分别输入年、月、日,判断这一天是当年的第几天
public class test1 { public static void main(String[] args) { /* 三、从键盘上输入某年的“month”和“day”,要求通过程序 输出输入的日期为2019年的第几天。 */ Scanner scan = new Scanner(System.in); System.out.println("请输入年:"); int year = scan.nextInt(); System.out.println("请输入月:"); int month = scan.nextInt(); System.out.println("请输入日:"); int day = scan.nextInt(); //闰年的判断方法:在java代码中使用if语句通过判断年份是否能被4整除, // 并且不能被100整除或是否能被400整除来判断是否是闰年。 boolean isRun = false; if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ isRun = true; } int count = 0; switch(month){ case 12: count += 30; case 11: count += 31; case 10: count += 30; case 9: count += 31; case 8: count += 31; case 7: count += 30; case 6: count += 31; case 5: count += 30; case 4: count += 31; case 3: if(isRun) { count += 29; }else{ count += 28; } case 2: count += 31; case 1: count += day; } System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + count + "天;"); } }