集册 Java实例教程 开启字串值

开启字串值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

581
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
开启字串值

public class StringSwitchDemo {
/** 
来 自 
时 代 J a v a 公 众 号
**/


    public static int getMonthNumber(String month) {


        int monthNumber = 0;


        if (month == null) {

            return monthNumber;

        }


        switch (month.toLowerCase()) {

        case "january":

            monthNumber = 1;

            break;

        case "february":

            monthNumber = 2;

            break;

        case "march":

            monthNumber = 3;

            break;
            /*
            来 自*
             时 代 J a v a
            */

        case "april":

            monthNumber = 4;

            break;

        case "may":

            monthNumber = 5;

            break;

        case "june":

            monthNumber = 6;

            break;

        case "july":

            monthNumber = 7;

            break;

        case "august":

            monthNumber = 8;

            break;

        case "september":

            monthNumber = 9;

            break;

        case "october":

            monthNumber = 10;

            break;

        case "november":

            monthNumber = 11;

            break;

        case "december":

            monthNumber = 12;

            break;

        default:

            monthNumber = 0;

            break;

        }


        return monthNumber;

    }


    public static void main(String[] args) {


        String month = "August";


        int returnedMonthNumber = StringSwitchDemo.getMonthNumber(month);


        if (returnedMonthNumber == 0) {

            System.out.println("Invalid month");

        } else {

            System.out.println(returnedMonthNumber);

        }

    }

}


展开阅读全文