Java:变量可能尚未初始化

时间:2017-09-15 22:52:35

标签: java compiler-errors

我是编程新手。环顾网络,无法解决这个问题。任何帮助或解释将不胜感激为什么它一直给我一个错误。

错误:可能尚未初始化变量天数

import java.util.Scanner;

public class month{
  public static void main (String[] args){

  Scanner keyboard = new Scanner(System.in);  
  int month, days;

  System.out.println("Enter the month: ");
  month = keyboard.nextInt();

  //month = ... // assume that we got this from the user
  if((month == 1)|| (month == 3) || (month == 5) || (month == 7) ||
     (month == 8) || (month == 10) || (month == 12))
    days = 31;
  else if ((month == 4)||(month == 6)||(month == 12))
    days = 30;
  else if (month == 2)
    days = 28;


  System.out.println("This month contains: "  +days + " days");
  }
}

1 个答案:

答案 0 :(得分:0)

如果没有满足任何条件,您应该包含else。这意味着days将始终初始化。