【问题标题】:Variable 'result' might not have been initialized变量“结果”可能尚未初始化
【发布时间】:2015-06-28 23:14:04
【问题描述】:

public class MonthName {
  public static String month_name(int month) {
    String result;

    if (month == 1) {
      result = "January";
    } else if (month == 2) {
      result = "February";
    } else if (month == 3) {
      result = "February";
    } else if (month == 4) {
      result = "February";
    } else if (month == 5) {
      result = "February";
    } else if (month == 6) {
      result = "February";
    } else if (month == 7) {
      result = "February";
    } else if (month == 8) {
      result = "February";
    } else if (month == 9) {
      result = "February";
    } else if (month == 10) {
      result = "February";
    } else if (month == 11) {
      result = "February";
    } else if (month == 12) {
      result = "February";
    }

    return result;
  }


  public static void main(String[] args) {
    System.out.println("Month 1: " + month_name(1));
    System.out.println("Month 2: " + month_name(2));
    System.out.println("Month 3: " + month_name(3));
    System.out.println("Month 4: " + month_name(4));
    System.out.println("Month 5: " + month_name(5));
    System.out.println("Month 6: " + month_name(6));
    System.out.println("Month 7: " + month_name(7));
    System.out.println("Month 8: " + month_name(8));
    System.out.println("Month 9: " + month_name(9));
    System.out.println("Month 10: " + month_name(10));
    System.out.println("Month 11: " + month_name(11));
    System.out.println("Month 12: " + month_name(12));
    System.out.println("Month 43: " + month_name(43));
  }
}

所以我已经在结果中声明了相关字符串的值,但它仍然说我的变量“结果”可能没有被初始化。

我正在尝试实现类似于this 的输出。 任何人都可以帮助我吗?谢谢!

【问题讨论】:

    标签: java


    【解决方案1】:

    如果您的变量月份可能为空或 12,则可能会发生这种情况。您可以通过简单地删除 if-else 树中的最后一个 if 来解决这个问题,例如:

    public class MonthName {
      public static String month_name(int month) {
        String result = "";
    
        if (month == 1) {
          result = "January";
        } else if (month == 2) {
          result = "February";
        } else if (month == 3) {
          result = "February";
        } else if (month == 4) {
          result = "February";
        } else if (month == 5) {
          result = "February";
        } else if (month == 6) {
          result = "February";
        } else if (month == 7) {
          result = "February";
        } else if (month == 8) {
          result = "February";
        } else if (month == 9) {
          result = "February";
        } else if (month == 10) {
          result = "February";
        } else if (month == 11) {
          result = "February";
        } else {
          result = "February";
        }
    
        return result;
      }
    
    
      public static void main(String[] args) {
        System.out.println("Month 1: " + month_name(1));
        System.out.println("Month 2: " + month_name(2));
        System.out.println("Month 3: " + month_name(3));
        System.out.println("Month 4: " + month_name(4));
        System.out.println("Month 5: " + month_name(5));
        System.out.println("Month 6: " + month_name(6));
        System.out.println("Month 7: " + month_name(7));
        System.out.println("Month 8: " + month_name(8));
        System.out.println("Month 9: " + month_name(9));
        System.out.println("Month 10: " + month_name(10));
        System.out.println("Month 11: " + month_name(11));
        System.out.println("Month 12: " + month_name(12));
        System.out.println("Month 43: " + month_name(43));
      }
    }

    这意味着您的变量必须介于 1 和 12 之间,否则您应该抛出异常。但无论如何,我不会一直使用那些 if-else 树。反正冗余太多了。

    改为这个:

    switch (month) {
    case 1:  result = "January";
             break;
    case 2:  result = "February";
             break;
    case 3:  result = "March";
             break;
    case 4:  result = "April";
             break;
    case 5:  result = "May";
             break;
    case 6:  result = "June";
             break;
    case 7:  result = "July";
             break;
    case 8:  result = "August";
             break;
    case 9:  result = "September";
             break;
    case 10: result = "October";
             break;
    case 11: result = "November";
             break;
    case 12: result = "December";
             break;
    default: result = "Invalid month";
             break;
    }
    

    【讨论】:

      【解决方案2】:

      局部变量应在使用前初始化。它们没有默认值

      初始化

      String result="";
      

      【讨论】:

        【解决方案3】:

        由于month 只是一个int,它可以采用1-12 以外的值(您的if/else/if 结构处理的内容)。

        要消除错误,您可以将其初始化为空的String,但是,您可能需要考虑错误情况。

        例如,如果传递了不在1-12 范围内的东西,该方法应该做什么?

        【讨论】:

          【解决方案4】:

          变量result 尚未初始化。 result 只会在输入介于 1 和 12 之间时包含一个值。

          你可以在声明变量的时候试试这个

          String result = "";
          

          这样您就不需要添加另一行来将结果设置为错误。

          String result = "error";
          

          【讨论】:

            猜你喜欢
            • 2012-03-25
            • 2015-07-04
            • 1970-01-01
            • 2013-11-24
            相关资源
            最近更新 更多