将If和else语句更改为Switch语句

时间:2018-05-03 17:00:51

标签: java switch-statement

所以我开始使用下面的代码制作日期打印机,我搞砸了那段代码,所以我决定尝试更改所有if和else语句来切换语句。代码假设采用两个整数并打印出日期。例如输入: 1 2 将打印:1月2日

我知道下面的代码不正确,但它可能有助于解释我想要做的事情。 (对不起,我对此很新)

    class DatePrinter {
    public static void main(String[] args) {
    int month, day;
    boolean dateIsValid = false;

    System.out.println("Enter two integers.  Press return after each 
    integer.");
    month = A7Helper.nextInteger();
    day = A7Helper.nextInteger();

    if (month == 4 && day >= 1 && day <= 30) {
        System.out.print("April ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 5 && day >= 1 && day <= 31) {
        System.out.print("May ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 7 && day >= 1 && day <= 30) {
        System.out.print("June ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 7 && day >= 1 && day <= 31) {
        System.out.print("July ");
        System.out.print(day);
        dateIsValid = true;

    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 8 && day >= 1 && day <= 31) {
        System.out.print("August ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 9 && day >= 1 && day <= 31) {
        System.out.print("September ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 10 && day >= 1 && day <= 31) {
        System.out.print("October ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 11 && day >= 1 && day <= 30) {
        System.out.print("November ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 12 && day >= 1 && day <= 31) {
        System.out.print("December ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 1 && day >= 1 && day <= 31) {
        System.out.print("January");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 2 && day >= 1 && day <= 28) {
        System.out.print("February ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 3 && day >= 1 && day <= 31) {
        System.out.print("March ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (day == 1) {
        System.out.println("th");
    } else {
        System.out.println("th");
    }

    if (day == 2) {
        System.out.println("nd");
    } else {
        System.out.println("th");
    }

    if (day == 3) {
        System.out.println("rd");
    } else {
        System.out.println("th");
    }
    }
    }

也是A7Helper

import java.util.Scanner;
public class A7Helper {
static Scanner keyboard = new Scanner(System.in);
static int nextInteger() {
return keyboard.nextInt();
}
}

1 个答案:

答案 0 :(得分:0)

以下是如何使用switch语句执行此操作:

dateIsValid = true;
switch (month) {
    case 2:
        maxday = 28;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        maxday = 30;
        break;
    default:
        if (month > 12) {
            dateIsValid = False;
        }
        maxday = 31;
        break;
}
if (day < 1 || day > maxday) {
    dateIsValid = false;
}
if (!dateIsValid) {
    System.out.println("Invalid Date");
}

请注意,代码仅适用于非闰年。案例2需要相应修复。

这对于学习如何编写代码很好,但是如果你真的需要在实际应用程序中处理日期,你应该使用一个专门用于此目的的Java库,因为时间总是比人们想象的更复杂。

我看到我也没有处理提供月份的名称。要做到这一点,只需为每个月制作一个单独的案例,如下所示:

case 1:
    monthname = "Jan"
    maxday = 31
    break;

monthnamemaxday的定义留给学生作为练习。 ; - )