将以下switch语句转换为if-else语句:

时间:2020-10-06 06:33:30

标签: java if-statement netbeans switch-statement

int month = input.nextInt();
switch (month) { 
    case 1:
        System.out.println(“January”);
    case 2:
        System.out.println(“February”); break;
    case 3:
        System.out.println(“March”); break;
    case 4:
        System.out.println(“April”);
    case 5:
        System.out.println(“May”); break;
    default:
        System.out.println(“Invalid”); break;
}

如何将其从switch语句转换为if-else语句?

休息是故意的,这是大学的作业。

2 个答案:

答案 0 :(得分:1)

在伪代码中,您可以不使用break语句而在单个语句中一起检查所有连接的部分,并在另一个嵌套的if语句中检查值。

if 1 or 2
    if 1 print january
    print february
else if 3 print march
else if 4 or 5 
    if 4 print april
    print may
else print invalid

答案 1 :(得分:0)

my_other_object

对于以后的情况,您应该可以重复该操作。请下次尝试清理您的代码,如果我们有时间回答您,至少应该花点时间正确询问,谢谢