Java - 从switch语句返回整数值

时间:2017-01-05 17:28:50

标签: switch-statement return

public class CS1702_Lab8 
{
public static void DaysInAMonth()
{
    int daysinmonth = 0;
    int days = Month(daysinmonth);
    System.out.println(days);
}
public static int Month(int daysinmonth)
{
    String month = "September";
    switch(month)
    {
        case "January": 
            daysinmonth = 31;
            break;
        case "February":
            daysinmonth = 28;
            break;
        case "March":
            daysinmonth = 31;
            break;
        case "April":
            daysinmonth = 30;
            break;
        case "May":
            daysinmonth = 31;
            break;
        case "June":
            daysinmonth = 30;
            break;
        case "July":
            daysinmonth = 31;
            break;
        case "August":
            daysinmonth = 31;
            break;
        case "September":
            daysinmonth = 30;
            break;
        case "October":
            daysinmonth = 31;
            break;
        case "November":
            daysinmonth = 30;
            break;
        case "December":
            daysinmonth = 31;
            break;
        default: 
            daysinmonth = -1;
            break;
    }
      return daysinmonth;
}
}

有人可以告诉我为什么没有打印吗?我在我的month方法中使用switch语句,并希望将daysinmonth整数值返回到public static void DaysInAMonth。我是Java的初学者,所以我提前为任何基本错误道歉。

1 个答案:

答案 0 :(得分:0)

简单地使用static void main(String[] args)而不是DaysInAMonth()。 所以整个改变只会是:

public static void DaysInAMonth()
{
    int daysinmonth = 0;
    int days = Month(daysinmonth);
    System.out.println(days);
}

public static void main(String[] args)
{
    int daysinmonth = 0;
    int days = Month(daysinmonth);
    System.out.println(days);
}

<强>为什么吗 仅仅因为每个程序都需要一个起点。在课程中没有任何主要方法,程序甚至不会启动。