【问题标题】:Best way to check multiple strings in an if statement在 if 语句中检查多个字符串的最佳方法
【发布时间】:2015-10-27 13:16:23
【问题描述】:

在 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)
}

但它看起来很糟糕

顺便说一句,我不会一遍又一遍地检查法国的原型代码,所以我进入每个欧元国家时都会先检查是否有更好的方法。

【问题讨论】:

  • 你的问题有点不清楚。这:usercountry.equals("FRA")||usercountry.equals("FRA") 与 usercountry.equals("FRA") 相同(与自身进行或运算不会影响其值)。那么在这些 or 中的每一个中,是应该改变 variable 还是字符串文字?也就是说,你想要usercountry.equals("FRA") || usercountry.equals("BEL"),还是usercountry.equals("FRA") || somethingelse.equals("FRA")?这些是不同的答案。
  • 将所有需要的匹配项放入List 并使用contains。如果List 不发生变化(很大),您可以将其维护为static 资源。
  • 为什么不使用switch statement。只需将所有案例的正文留空,没有break,除了最后一个匹配案例,并提供定义:-)
  • Set 而不是List。

标签: java string if-statement


【解决方案1】:

如果您使用的是 Java 7 或更高版本,您可以像这样在字符串上使用 switch 语句

switch(userCountry)
{
    case "FRA":
        costpermile = costpermile*1.42;
        break;
    default:
        break;
}

然后您可以添加您需要的任何其他案例。

【讨论】:

  • 不是不同意,但需要 26 次以上的匹配,这将是一个相当长的转换;)
  • 完全同意。案例太多,会有点笨拙
  • break 分支中的 break 语句不是必需的 :) 在你的情况下
【解决方案2】:

您可以将字符串存储在一个数组中,然后像这样对其进行迭代:

String[] str = {"EN", "FRA", "GER"};
for (String s : str) {
    if (usercountry.equals(s)) {
        // Match: do something...
    }
}

【讨论】:

  • 恕我直言-List#contains 需要列表代码,只是说;)
  • @MadProgrammer 你确定,你想回复我的回答吗? ;)
  • 这并不是对您的回答的批评,但我更喜欢List#contains 而不是增强的循环,但这只是我 ;)
  • 嗯,好的。现在,我明白了(你的评论和我的回答之间的关系我不清楚)。是的,你可能是对的 :)
【解决方案3】:

1.正则表达式

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

2。将国家添加到数据结构(集)并检查

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

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

注意:我认为它是您正在寻找的正则表达式方法

【讨论】:

    【解决方案4】:

    你可以这样做:

    String euroCountries []  = {"FRA", "DEU", ...}
    
    public boolean isEuroCountry(String userCountry){
      for(String country : euroCountries){
        if(usercountry.equals(country)){
             return true;
        }
      }
      return false;
    }
    

    【讨论】:

      【解决方案5】:

      正如其他人所建议的,您可以使用 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
      }
      

      但是,更好的长期解决方案可能是创建一个丰富的enum,尤其是当您需要为这些国家/地区代码附加更多逻辑时。

      【讨论】:

        【解决方案6】:

        您可以为属于特定大陆的每个国家/地区附加一个前缀,然后检查该标记。 例如在欧洲国家:

        • 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
        }
        ...
        

        【讨论】:

          【解决方案7】:
          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
          
                  }
          
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-04-01
            • 1970-01-01
            • 2015-12-02
            • 1970-01-01
            • 2022-08-18
            • 1970-01-01
            • 2017-03-09
            • 2017-10-25
            相关资源
            最近更新 更多