iT邦幫忙

2021 iThome 鐵人賽

DAY 7
0
自我挑戰組

C# 雞礎觀念系列 第 7

[Day7]C# 雞礎觀念- 你有選擇障礙嗎? 交給 if陳述式 解決吧!

在生活中,我們常常面臨選擇,
今天午餐吃什麼好呢?飯還是麵呢?
明天要去哪裡玩?山上還是海邊呢?
那這些選擇的邏輯結構在C#中我們稱為選取範圍語句

if陳述式/images/emoticon/emoticon50.gif

  • 在日常對話中,我們常常說,如果...就...
  • 如果下雨,就待在家
  • 在C#的表達方式則是,if....else...
    它的樣子會是

if ( 條件 )
{
..............
}
else
{
...............
}

範例1:如果下雨,那就待在家,沒下雨就出去玩

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告天氣
            string weather = "下雨";
            Console.WriteLine("今天天氣 : " + weather);

            //使用if陳述句來做判斷
            if(weather == "下雨")
            {
                Console.WriteLine("待在家");
            }
            else
            {
                Console.WriteLine("出去玩");
            }

            //把天氣改成晴天,再做一次判斷試試看
            weather = "晴天";
            Console.WriteLine("今天天氣 : " + weather);

            //使用if陳述句來做判斷
            if (weather == "下雨")
            {
                Console.WriteLine("待在家");
            }
            else
            {
                Console.WriteLine("出去玩");
            }

            Console.ReadKey();
        }
    }
}

結果:

今天天氣 : 下雨
待在家
今天天氣 : 晴天
出去玩

判斷數字的大小/images/emoticon/emoticon09.gif

範例:招牌上面寫說未成年請勿進入,那我們來做一個輸入年齡,讓電腦判斷能不能進入吧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入年齡");
            //宣告年齡,並將輸入的值賦予給他
            int age = Convert.ToInt32(Console.ReadLine());

            //判斷年齡
            if(age < 18)
            {
                Console.WriteLine(age + " 歲是未成年,不可進入!");
            }
            else
            {
                Console.WriteLine(age + " 歲已成年,歡迎光臨!");
            }

            Console.ReadKey();
        }
    }
 }

輸入:

48

結果:

請輸入年齡
48
48 歲已成年,歡迎光臨!

輸入:

15

結果:

請輸入年齡
15
15 歲是未成年,不可進入!

else if/images/emoticon/emoticon37.gif

  • 如果有多個要判斷的條件我們可以搭配else if做使用

範例:如果我滿18歲就可以考機車駕照,但要滿20歲才能考重機駕照

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入年齡");
            //宣告年齡,並將輸入的值賦予給他
            int age = Convert.ToInt32(Console.ReadLine());

            //判斷年齡
            if (age < 18)
            {
                Console.WriteLine(age + " 歲是未成年,任何駕照都不能考!");
            }
            else if (age < 20)
            {
                Console.WriteLine(age + " 歲,可以考機車駕照,但不能考重機駕照!");
            }
            else
            {
                Console.WriteLine(age + " 歲,任何駕照都可以考!");
            }
           Console.ReadKey();
        }
    }
  }

輸入:

15

結果:

請輸入年齡
15
15 歲是未成年,任何駕照都不能考!

輸入:

19

結果:

請輸入年齡
19
19 歲,可以考機車駕照,但不能考重機駕照!

輸入:

48

結果:

請輸入年齡
48
48 歲,任何駕照都可以考!

巢狀if

  • 我們可能會先第一個決定,之後再做第二個決定

範例:如果是假日我就要出去玩,平日就得乖乖上課,如果晴天我就要去海邊,不然就去爬山

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //今天
            Console.WriteLine("今天星期幾?");
            string today = Console.ReadLine();
            //天氣
            Console.WriteLine("今天天氣?");
            string weather = Console.ReadLine();

            //先判斷今天日是不是假日
            if (today == "星期六" || today == "星期日")
            {
                if(weather == "晴天")
                {
                    Console.WriteLine("今天" + today + "," + weather + ",來去海邊玩");
                }
                else
                {
                    Console.WriteLine("今天" + today + "," + weather + ",來去爬山");
                }
            }
            else
            {
                Console.WriteLine("今天" + today + ",所以要上課");
            }

            Console.ReadKey();
        }
    }
 }

輸入:

星期六
晴天

結果:

今天星期幾?
星期六
今天天氣?
晴天
今天星期六,晴天,來去海邊玩

輸入:

星期日
陰天

結果:

今天星期幾?
星期日
今天天氣?
陰天
今天星期日,陰天,來去爬山

輸入:

星期一
陰天

結果:

今天星期幾?
星期一
今天天氣?
陰天
今天星期一,所以要上課

自從學會if之後再也沒有選擇障礙了,今天的故事就到這邊了,謝謝大家,我們明天再見


上一篇
[Day6]C# 雞礎觀念- 程式碼擁有判斷真假的能力~邏輯運算子
下一篇
[Day8]C# 雞礎觀念- 如果沒有如果,只有太多的如果switch陳述式
系列文
C# 雞礎觀念32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言