iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
Software Development

C# 入门系列 第 5

C# 入门之逻辑判断(下)

  • 分享至 

  • xImage
  •  

前面我们介绍了简单的条件逻辑运算符,现在我们来看一下另外一种条件判断 ———— if.

在前面的判断一个数是否在 0 到 5 之间,我们通过 “between0And5” 的输出是否为 TRUE 来判断这个数是否在 0 到 5 之间。我们可以使用 if 改写这个程序,让程序之间输出这个数是否在 0 到 5 之间:

using System;

namespace Operator3
{
    class Program
    {
        static void Main(string[] args)
        {
            int myNum;
            Console.WriteLine("Please type a number:");
            myNum = Convert.ToInt32(Console.ReadLine());
            if ((0 <= myNum) && (myNum <= 5))
                    Console.WriteLine("The number is between 0 and 5");
        }
    }
}

运算结果:
https://ithelp.ithome.com.tw/upload/images/20210903/200994948hiYtI2xX3.png

但如果我输入的数不是一个 0 到 5 之间的数,会怎么样:
https://ithelp.ithome.com.tw/upload/images/20210903/20099494ELBLucRqCw.png

没有任何返回结果,为了防止出现这种情况,我们还需要加一个分支:

using System;

namespace Operator3
{
    class Program
    {
        static void Main(string[] args)
        {
            int myNum;
            Console.WriteLine("Please type a number:");
            myNum = Convert.ToInt32(Console.ReadLine());
            if ((0 <= myNum) && (myNum <= 5))
                Console.WriteLine("The number is between 0 and 5");
            else
                Console.WriteLine("The number isn't between 0 and 5");
        }
    }
}

再次运行这个程序,现在输入一个不在 0 到 5之间的数:
https://ithelp.ithome.com.tw/upload/images/20210903/20099494GU6g7ihgy4.png

上面的示例表现了两个分支的情况,但在某些情况下,我们需要的不止两个分支,需要三个,甚至更多:

using System;

namespace Operator3
{
    class Program
    {
        static void Main(string[] args)
        {
            int myNum;
            Console.WriteLine("1. This is Active Directory.");
            Console.WriteLine("2. This is Hyper-v.");
            Console.WriteLine("3. This is DHCP.");
            Console.WriteLine("4. This is DNS.");
            Console.WriteLine( "");
            Console.WriteLine("Select a service you want to maintenance");
            myNum = Convert.ToInt32(Console.ReadLine());
            if (myNum == 1)
                Console.WriteLine("The service you want to maintenance is Active Directory.");
            else if (myNum == 2)
                Console.WriteLine("The service you want to maintenance is Hyper-v.");
            else if (myNum == 3)
                Console.WriteLine("The service you want to maintenance is DHCP.");
            else if (myNum == 4)
                Console.WriteLine("The service you want to maintenance is DNS.");
            else
                Console.WriteLine("input error.");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210903/20099494T5zMCg8c2G.png

对于上面的示例,我们还可以通过 switch 的方式来写:

using System;

namespace Operator3
{
    class Program
    {
        static void Main(string[] args)
        {
            int myNum;
            Console.WriteLine("1. This is Active Directory.");
            Console.WriteLine("2. This is Hyper-v.");
            Console.WriteLine("3. This is DHCP.");
            Console.WriteLine("4. This is DNS.");
            Console.WriteLine( "");
            Console.WriteLine("Select a service you want to maintenance");
            myNum = Convert.ToInt32(Console.ReadLine());
            switch ( myNum )
            {
                case 1:
                    Console.WriteLine("The service you want to maintenance is Active Directory.");
                    break;
                case 2:
                    Console.WriteLine("The service you want to maintenance is Hyper-v.");
                    break;
                case 3:
                    Console.WriteLine("The service you want to maintenance is DHCP.");
                    break;
                case 4:
                    Console.WriteLine("The service you want to maintenance is DNS.");
                    break;
                default:
                    Console.WriteLine("input error.");
                    break;
            }
        }
    }
}

运算结果:
https://ithelp.ithome.com.tw/upload/images/20210903/20099494rfykGDKk9L.png

小贴士:
在 switch 语句中,没个分支中的 break 是必须的,不然会挨个执行所有分支,有兴趣的可以自己尝试一下,删除 break,然后执行程序。关于 break 的更多内容,我们会在后面讲循环时讲到。


上一篇
C# 入门之逻辑判断(上)
下一篇
C# 入门之循环
系列文
C# 入门32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言