集册 Java实例教程 如果条件测验

如果条件测验

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

508
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
以一个月和一年作为参数,并显示该月中的天数。

public class Main {

    public static void main(String[] arguments) {//时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提 供

        int yearIn = 2012;

        int monthIn = 1;

        if (arguments.length > 0)

            monthIn = Integer.parseInt(arguments[0]);

        if (arguments.length > 1)

            yearIn = Integer.parseInt(arguments[1]);

        System.out.println(monthIn + "/" + yearIn + " has "

            + countDays(monthIn, yearIn) + " days.");

    }


    static int countDays(int month, int year) {

        int count = -1;/**from 时 代 J a v a - N o w J a v a . c o m**/

        switch (month) {

            case 1:

            case 3:

            case 5:

            case 7:

            case 8:

            case 10:

            case 12:

                count = 31;

                break;

            case 4:

            case 6:

            case 9:

            case 11:

                count = 30;

                break;

            case 2:

                
展开阅读全文