/* 典型的Switch-case程序题: 成绩等级划分: >= 90 A >= 80 B >= 70 C >= 60 D < 60 E */ import java.util.Scanner; public class SwitchInt { public static void main(String[] args) { double score; Scanner scan = new Scanner(System.in); score = scan.nextDouble(); switch ((int) (score / 10)) { case 10: case 9: System.out.println("A"); break; case 8: System.out.println("B"); break; case 7: System.out.println("C"); break; case 6: System.out.println("D"); break; default: System.out.println("E"); //default下,就不用break了 } } }
//在Java中switch-case除了可以比较int外,还可以用来比较String: import java.util.Scanner; public class SwitchString { public static void main(String[] args) { String month; Scanner scan = new Scanner(System.in); System.out.print("input month (january, march, etc): "); month = scan.next(); switch (month) { case "december": case "january": case "february": System.out.println("winter"); break; case "march": case "april": case "may": System.out.println("spring"); break; case "june": case "july": case "august": System.out.println("summer"); break; case "september": case "october": case "november": System.out.println("autumn/fall"); break; default: System.out.println("invalid month, check again."); } } }