在if语句中检查多个字符串的最佳方法

3

什么是在if语句中检查多个字符串的最干净的方法,我想要能够检查用户所在的国家是否使用欧元,我将把这些国家放在("???")中。因为这样可以起作用。

if (usercountry.equals("FRA") || usercountry.equals("FRA")|| 
    usercountry.equals("FRA") || usercountry.equals("FRA") || 
    usercountry.equals("FRA") || usercountry.equals("FRA") || 
    usercountry.equals("FRA") || usercountry.equals("FRA") ||
    usercountry.equals("FRA")) {
        costpermile = costpermile*1.42;   //(costpermile=£)   (costpermile*1.42=Euros)
}

但是它看起来很糟糕。

顺便说一下,我并不会反复检查法国,因为这仍然是原型代码,所以我还没有输入每个欧元区国家,而是先检查是否有更好的方法。


1
你的问题有点不清楚。这个:usercountry.equals("FRA")||usercountry.equals("FRA")usercountry.equals("FRA") 是一样的(将某个东西与自身进行OR运算不会影响其值)。所以在每一个或者中,是变量应该改变还是字符串常量?也就是说,你想要 usercountry.equals("FRA") || usercountry.equals("BEL"),还是 usercountry.equals("FRA") || somethingelse.equals("FRA")?这些是不同的答案。 - yshavit
2
将所有所需的匹配项放入一个“List”中,并使用“contains”。如果不会(大幅)更改,您可以将“List”作为静态资源维护。 - MadProgrammer
1
为什么不使用switch语句。只需将所有情况的主体留空,没有break,除了最后一个匹配的情况,并提供定义 :-) - nIcE cOw
1
使用 Set 而不是 List - RealSkeptic
7个回答

4

1. 正则表达式

if (usercountry.matches("FRA|GER|ITA"))
{
    costpermile = costpermile*1.42; 
}

2. 将国家添加到数据结构(Set)中并进行检查

Set<String> eurocountries= new HashSet<String>();
eurocountries.add("FRA");eurocountries.add("GER");eurocountries.add("ITA");

if (eurocountries.contains(usercountry))
{
    costpermile = costpermile*1.42; 
}

注意:我认为你要找的是正则表达式方法。

1
如果您正在使用Java 7或更高版本,您可以像这样在字符串上使用switch语句。
switch(userCountry)
{
    case "FRA":
        costpermile = costpermile*1.42;
        break;
    default:
        break;
}

然后,您只需添加所需的其他情况即可。

不是不同意,但如果需要26个以上的匹配,那将会是一个非常长的开关语句 ;) - MadProgrammer
完全同意。太多的情况会让它有点笨重。 - mcarlin
default 分支中使用 break 语句并不是必须的 :) 在您的情况下 - Andrew Tobilko

1
你可以将字符串存储在数组中,然后像这样迭代它:
String[] str = {"EN", "FRA", "GER"};
for (String s : str) {
    if (usercountry.equals(s)) {
        // Match: do something...
    }
}

在我看来,List#contains需要列表代码,只是说一下而已 ;) - MadProgrammer
@MadProgrammer 你确定你想回复我的答案吗?;) - pzaenger
这并不是对你的回答的批评,但我更喜欢使用List#contains而不是增强型循环,但这只是我的个人偏好 ;) - MadProgrammer
啊,好的。现在我明白了(你的评论和我的回答之间的关系对我来说不太清楚)。是的,你可能是对的 :) - pzaenger

1

正如其他人建议的那样,您可以使用Set存储国家代码:

private static final Set<String> EURO_COUNTRIES 
    = new HashSet<>(Arrays.asList("FRA", "ESP", "ITA", "GER" /*etc..*/));

然后在您的代码中,您可以按以下方式检查国家:
String userCountry = Locale.getDefault().getISO3Country();

if (EURO_COUNTRIES.contains(userCountry)) {
    // do something
}

然而,更好的长期解决方案可能是创建一个丰富的枚举,特别是如果您需要将更多逻辑与这些国家代码相关联。

0
你可以像这样做:
String euroCountries []  = {"FRA", "DEU", ...}

public boolean isEuroCountry(String userCountry){
  for(String country : euroCountries){
    if(usercountry.equals(country)){
         return true;
    }
  }
  return false;
}

0

您可以为每个属于特定大陆的国家添加前缀,然后只需检查该令牌即可。

例如,在欧洲国家:
  • E_FRA
  • E_BEL
  • E_GER
  • ...

将是E

亚洲国家:

  • A_CHN
  • A_MLY
  • A_PHL
  • ...

将是A,以此类推。

if ( userCountry.startsWith("E") ) {
     // Europe countries
} else
if ( userCountry.startsWith("A") ) {
    // Asian countries
}
...

0
String countries[] = {"FRA", "GER", "ENG"} // declaration of the countries you want to list.

// function to calculate the cost per mile 
public double calculateCostPerMile(String userCountry){
    double costPerMile;
    for(String country: countries){
        if(country.equals(userCountry)){
            return costPerMile*1.42; // return value

        }

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接