使用 java - 在 switch 中使用 if else 语句?
Using java - using if else statement in a switch?
我对 Java 还很陌生,一般来说编码。在我的 class 中,我们需要用户输入月份 (1-12) 和日期 (1-30),然后我们在设定的日期(6 月 15 日至 9 月 30 日)确定是否是季风季节.我试图在 switch 中使用 if else 语句来说明 6 月 15 日之前的任何时候都不是季风季节,但我的代码一直显示 if 和 else 语句。欢迎任何帮助,谢谢!
import java.util.*;
public class Monsoon
{ public static void main (String[]args)
{
Scanner kb = new Scanner (System.in);
// Prompt the user to enter month & Day
System.out.print("Please enter a month(1-12) and day (1-31): " );
int month = kb.nextInt();
int day = kb.nextInt();
// using a switch statement to show months
switch (month){
case 1:
System.out.println("is NOT monsoon season");
break;
case 2:
System.out.println("is NOT monsoon season");
break;
case 3:
System.out.println("is NOT monsoon season");
break;
case 4:
System.out.println("is NOT monsoon season");
break;
case 5:
System.out.println("is NOT monsoon season");
break;
case 6:
System.out.println("“is monsoon season");
// use if else statement so user knows that before june 15 is not monsoon season
if (day>=15)
System.out.print("it is monsoon season");
else
System.out.print("it is not monsoon season");
break;
case 7:
System.out.println("“is monsoon season");
break;
case 8:
System.out.println("“is monsoon season");
break;
case 9:
System.out.println("“is monsoon season");
break;
case 10:
System.out.println("is NOT monsoon season");
break;
case 11:
System.out.println("is NOT monsoon season");
break;
case 12:
System.out.println("is NOT monsoon season");
break;
default: System.out.println("not valid");
break;
}
}
}
重复不好。你可以这样做,利用 switch-case 的 fall-through 特性:
boolean isMonsoon;
switch (month) {
case 7:
case 8:
case 9:
isMonsoon = true;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 10:
case 11:
case 12:
isMonsoon = false;
break;
case 6:
// use if else statement so user knows that before june 15 is not monsoon season
if (day >= 15)
isMonsoon = true;
else
isMonsoon = false;
break;
default:
System.out.println("not valid");
break;
}
if (isMonsoon)
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");
或者,由于季风季节是 范围,使用比较运算符可能比 switch-case 更合适:
if ((month >= 7 && month < 10) || (month == 6 && day >= 15))
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");
我对 Java 还很陌生,一般来说编码。在我的 class 中,我们需要用户输入月份 (1-12) 和日期 (1-30),然后我们在设定的日期(6 月 15 日至 9 月 30 日)确定是否是季风季节.我试图在 switch 中使用 if else 语句来说明 6 月 15 日之前的任何时候都不是季风季节,但我的代码一直显示 if 和 else 语句。欢迎任何帮助,谢谢!
import java.util.*;
public class Monsoon
{ public static void main (String[]args)
{
Scanner kb = new Scanner (System.in);
// Prompt the user to enter month & Day
System.out.print("Please enter a month(1-12) and day (1-31): " );
int month = kb.nextInt();
int day = kb.nextInt();
// using a switch statement to show months
switch (month){
case 1:
System.out.println("is NOT monsoon season");
break;
case 2:
System.out.println("is NOT monsoon season");
break;
case 3:
System.out.println("is NOT monsoon season");
break;
case 4:
System.out.println("is NOT monsoon season");
break;
case 5:
System.out.println("is NOT monsoon season");
break;
case 6:
System.out.println("“is monsoon season");
// use if else statement so user knows that before june 15 is not monsoon season
if (day>=15)
System.out.print("it is monsoon season");
else
System.out.print("it is not monsoon season");
break;
case 7:
System.out.println("“is monsoon season");
break;
case 8:
System.out.println("“is monsoon season");
break;
case 9:
System.out.println("“is monsoon season");
break;
case 10:
System.out.println("is NOT monsoon season");
break;
case 11:
System.out.println("is NOT monsoon season");
break;
case 12:
System.out.println("is NOT monsoon season");
break;
default: System.out.println("not valid");
break;
}
}
}
重复不好。你可以这样做,利用 switch-case 的 fall-through 特性:
boolean isMonsoon;
switch (month) {
case 7:
case 8:
case 9:
isMonsoon = true;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 10:
case 11:
case 12:
isMonsoon = false;
break;
case 6:
// use if else statement so user knows that before june 15 is not monsoon season
if (day >= 15)
isMonsoon = true;
else
isMonsoon = false;
break;
default:
System.out.println("not valid");
break;
}
if (isMonsoon)
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");
或者,由于季风季节是 范围,使用比较运算符可能比 switch-case 更合适:
if ((month >= 7 && month < 10) || (month == 6 && day >= 15))
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");