Switch能否使用String做参数

mac2022-06-30  63

在Java语言中Swith可以使用参数类型有:Only convertible int values, strings or enum variables are permitted

可以自动转换为整型的(byte,short,int),String类型,枚举类型。

Java中不能做为Switch参数的有boolean,float,double,long(不能直接转换为int啊)。

public class Main { public static void main(String[] args) { String s="abc"; switch(s){ case("abc"): System.out.println("i love xingxing"); break; case("123"): System.out.println("i love jiming"); break; default: break; } } }

 

转载于:https://www.cnblogs.com/hupp/p/4846580.html

相关资源:在java switch中使用String作为分支条件
最新回复(0)