【问题标题】:Better alternative to long list of if statements长长的 if 语句列表的更好替代方案
【发布时间】:2016-03-04 18:37:58
【问题描述】:

我有一个名为 dayCounter 的整数,它运行 1 到 7。我有一个名为 day 的字符串。 dayCounter 使用 TimerTask 每 10 秒递增一次。

我在用于TimerTask:: 的private void run() 方法中使用以下if 语句:

if (dayCounter == 1) {
    day = "Monday";
}

对于一周中的每一天,我都有一个这样的 if 语句。有没有更有效的方法可以得到相同的结果。我在想也许创建一个包含字符串月份的数组,然后以某种方式增加它。但我不知道该怎么做。有什么想法吗?

【问题讨论】:

    标签: java arrays if-statement


    【解决方案1】:

    除了 if 语句,您可以使用带计数器的 switch case

    switch(dayCounter){
        case 1:
            day = "Monday";
            break;
        case 2: 
            day = "Tuesday";
            break;
    }
    

    虽然这是另一种方法,但它们都创建相同的字节码,这两个选项之间的唯一区别是代码的可读性。

    【讨论】:

      【解决方案2】:

      可能最简单的方法是使用数组来表示日期:

      public class Demo {
      
           public static void main(String[] args) {
              System.out.println(getDay(1));
              System.out.println(getDay(7));
           }
      
           // Returns the corresponding day for the given dayCounter.
           // Assumes that dayCounter goes from 1-7.
           private static String getDay(int dayCounter) {
               String[] days = new String[] { "Monday", "Tuesday", "Wednesday",
                     "Thursday", "Friday", "Saturday", "Sunday" };
               return days[dayCounter - 1];
           }
      }
      

      如果您的计数器超过 7(例如,您在每个计时器步骤中将其递增 1),您可能希望对这个版本感兴趣,它基本上会循环所有可用的日期:

      public class Demo {
      
           public static void main(String[] args) {
              System.out.println(getDay(1));
              System.out.println(getDay(7));
              System.out.println(getDay(8));
              System.out.println(getDay(13));
           }
      
           // Returns the corresponding day for the given dayCounter.
           // Maps dayCounter to the range of 1-7, assumes that dayCounter
           // goes from 1-Integer.MAX_VALUE.
           private static String getDay(int dayCounter) {
               String[] days = new String[] { "Monday", "Tuesday", "Wednesday",
                     "Thursday", "Friday", "Saturday", "Sunday" };
               return days[dayCounter % 7];
           }
      }
      

      或者您可以使用DateFormatSymbols 检索工作日,甚至(几乎)自动为您翻译:

      import java.text.DateFormatSymbols;
      
      public class Demo {
      
           public static void main(String[] args) {
              System.out.println(getDay(1));
              System.out.println(getDay(7));
           }
      
           // Returns the corresponding day for the given dayCounter.
           // Assumes that dayCounter goes from 1-7.
           private static String getDay(int dayCounter) {
              String[] days = DateFormatSymbols.getInstance().getWeekdays();
              return days[dayCounter];
           }
      }
      

      或者对于 Java 8 及更高版本,当前首选的方法可能是使用新的java.time API(您可以使用方法参数)。此 API 包含一个方便的 DayOfWeek 枚举。

      import java.time.DayOfWeek;
      import java.time.format.TextStyle;
      import java.util.Locale;
      
      public class Demo {
      
          public static void main(String[] args) {
              System.out.println(getDay(1));
              System.out.println(getDay(7));
          }
      
          // Returns the corresponding day for the given dayCounter.
          // Assumes that dayCounter goes from 1-7.
          private static String getDay(int dayCounter) {
              return DayOfWeek.of(dayCounter)
                       .getDisplayName(TextStyle.FULL, Locale.getDefault());
          }
      
      }
      

      如果您只想替换 if 语句,请使用 Ryan 的回答中提到的 switch-case

      【讨论】:

      • 除非您预计一周中的几天会有所变化,否则最好使用枚举。
      • @azurefrog:我实际上是从枚举开始的,但我认为它可能太复杂了。特别是当已经有一个用于该目的的内置枚举时。不过,总的来说这是一个有效的观点。
      【解决方案3】:

      你可以有一个字符串数组,dayOfWeek,一个用于一周中的每一天。然后使用 dayCounter 作为数组的索引。例如,如果 dayCounter = 1,那么 dayOfWeek[dayCounter] 将是“星期一”。这样,您只需要一条语句即可获取星期几的名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-21
        • 1970-01-01
        • 2012-04-19
        • 2012-10-10
        • 1970-01-01
        • 2010-11-15
        相关资源
        最近更新 更多