Java IF語句 Part03

/*
    需求:
        判斷當前的天氣:
            當外邊下雨的時候:
                帶雨傘:
                    判斷性別:
                        當性別為男:帶一把大黑傘
                        當性別為女:帶一把小花傘
            當外邊是晴天的時候:
                判斷天氣溫度:
                    當天氣溫度在30度以上的時候:
                        性別為男:戴墨鏡
                        性別為女:擦防曬霜
*/

public class IfTest03{
    public static void main(String[] args){

        //我的代碼:
/*
        java.util.Scanner s = new java.util.Scanner(System.in);
        System.out.print("請輸入外面的天氣狀況【雨天:rain / 晴天:sun】:");
        String weather = s.next();
        //System.out.println(weather);//調(diào)試

        System.out.print("請輸入您的性別【男:man / 女:woman】:");
        String sex = s.next();
        //System.out.println(sex);//調(diào)試

        System.out.print("請輸入現(xiàn)在的天氣溫度【直接輸入數(shù)字即可】:");
        int temperature = s.nextInt();
        //System.out.println(temperature);//調(diào)試

        if((weather.equals("rain")) && (sex.equals("sun"))){
            System.out.println("您需要帶一把大黑傘");
        }else if((weather.equals("rain")) && (sex.equals("woman"))){
            System.out.println("您需要帶一把小花傘");
        }else if((weather.equals("sun")) && (temperature > 30) && (sex.equals("man"))){
            System.out.println("您需要戴墨鏡");
        }else if((weather.equals("sun")) && (temperature > 30) && (sex.equals("woman"))){
            System.out.println("您需要涂防曬霜");
        }else{
            System.out.println("對不起,您的輸入不正確,或系統(tǒng)中無此選項!");
        }
        
        */

        //自我進行代碼優(yōu)化,使用嵌套
/*
        java.util.Scanner s = new java.util.Scanner(System.in);
        System.out.print("請輸入外面的天氣狀況【雨天:rain / 晴天:sun】:");
        String weather = s.next();

        System.out.print("請輸入您的性別【男:man / 女:woman】:");
        String sex = s.next();

        System.out.print("請輸入現(xiàn)在的天氣溫度【直接輸入數(shù)字即可】:");
        int temperature = s.nextInt();

        if((weather.equals("rain") || weather.equals("sun")) && ((sex.equals("man")) || sex.equals("woman"))){
            if(weather.equals("rain")){
                if(sex.equals("man")){
                    System.out.println("您需要帶一把大黑傘");
                }else{
                    System.out.println("您需要帶一把小花傘");
                }
            }else if(weather.equals("sun")){
                if(temperature > 30){
                    if(sex.equals("man")){
                        System.out.println("您需要戴墨鏡");
                    }else{
                        System.out.println("您需要涂防曬霜");
                    }
                }else{
                    System.out.println("對不起,您的輸入不正確,或系統(tǒng)中無此選項!");
                }
            }
        }else{
            System.out.println("對不起,您的輸入不正確,或系統(tǒng)中無此選項!");
        }
        
*/
        //代碼優(yōu)化

        /*
        提示:
            1、要用到嵌套
            2、天氣狀況、溫度、性別都需要從鍵盤輸入
                天氣狀況:1表示下雨、0表示晴天
                溫度直接使用數(shù)字即可
                性別:1表示男、0表示女
        */
        java.util.Scanner s = new java.util.Scanner(System.in);

        System.out.println("歡迎使用本系統(tǒng),您通過本系統(tǒng)可以完成一些簡單的判斷:");
        System.out.println("說明1:1表示下雨,0表示晴天");
        System.out.println("說明2:1表示男,0表示女");
        System.out.println("說明3:溫度為數(shù)字");

        //接收天氣狀況
        System.out.print("請輸入當前天氣狀況:");
        int weather = s.nextInt();
        System.out.print("請輸入您的性別:");
        int sex = s.nextInt();

        //判斷天氣狀況
        if(weather == 1){
            //下雨天
            //System.out.println("下雨天");
            if(sex == 1){
                System.out.println("您需要帶一把大黑傘");
            }else if(sex == 0){
                System.out.println("您需要帶一把小花傘");
            }else{
                System.out.println("您的性別是怎么回事?");
            }

        }else if(weather == 0){
            System.out.print("請輸入當前的天氣溫度:");
            int temperature = s.nextInt();
            if(temperature >30){
                if(sex == 1){
                    System.out.println("您需要戴一副墨鏡");
                }else if(sex == 0){
                    System.out.println("您需要涂防曬霜");
                }else{
                    System.out.println("您的性別是怎么回事?");
                }
            }else{
                System.out.println("對不起,沒有這樣的溫度選項!");
            }
            //晴天
            //System.out.println("晴天");
        }else{
            System.out.println("對不起,您輸入的天氣狀況不正確!");
        }
    }
}
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容