繁体   English   中英

使用Java-在开关中使用if else语句吗?

[英]Using java - using if else statement in a switch?

我是Java的新手,并且一般来说都是编码。 在我的课程中,我们需要用户输入月份(1-12)和日期(1-30),然后在设定的日期(6月15日至9月30日)确定是否是季风季节。 我试图在开关中使用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;

    }

}

}

重复是不好的。 您可以使用开关盒的掉线功能来做到这一点:

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");

或者,由于季风季节是一个范围 ,因此使用比较运算符可能比切换情况更合适:

if ((month >= 7 && month < 10) || (month == 6 && day >= 15))
    System.out.println("is monsoon season");
else
    System.out.println("is NOT monsoon season");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM