在java代码中,switch语句以传入的字符串参数作为判断条件,在对应的case子句中使用的是字符串常量。
1 public class JavaDemo { 2 3 public static void main(String[] args) { 4 System.out.println(new Title().generate("baorant", "男")); 5 6 } 7 8 } 9 10 class Title { 11 public String generate(String name, String gender) { 12 String title = ""; 13 switch (gender) { 14 case "男": 15 title = name + "先生"; 16 break; 17 case "女": 18 title = name + "女士"; 19 break; 20 default: 21 title = name; 22 } 23 return title; 24 } 25 }