我正在编写的Java程序的一部分会询问用户的祖国。另一部分使用switch语句,我得到了一个错误。错误是:The operator || is undefined for the argument type(s) java.lang.String, java.lang.String。下面是出现问题的方法:
public static String getCountryMessage(String countryName) {
switch (countryName) {
case "USA":
return "Hello, ";
case "England" || "UK":
return "Hallo, ";
case "Spain":
return "Hola, ";
case "France":
return "Bonjour, ";
case "Germany":
return "Guten tag, ";
default:
return "Hello, ";
}
}如何在Java switch语句中使用&&和||?
发布于 2013-02-24 05:07:52
我不认为您可以在switch语句中使用这样的条件语句。相反,编写以下代码会更简单、更直接:
case "England":
case "UK":
return "Hallo";这是一个fall-through case -如果你的字符串匹配英格兰或者英国,它将返回Hallo。
发布于 2013-02-24 05:08:05
使用直通案例:
case "England":
case "UK":
return "Hallo, ";https://stackoverflow.com/questions/15045695
复制相似问题