问题描述
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
语句?
休息是故意的,这是大学的作业。
解决方法
在伪代码中,您可以不使用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
,
my_other_object
对于以后的情况,您应该可以重复该操作。请下次尝试清理您的代码,如果我们有时间回答您,至少应该花点时间正确询问,谢谢