随便玩玩之C# 6 程序控制-条件判断if

随便玩玩之C# 6 程序控制-条件判断if

1.if

前面的内容中语句都是一条一条执行的。生活中又很多不确定的事情,就要做好各种打算。比如:如果明天下雨,在家打游戏。这里关键词就是“如果”,根据明天的实际情况,再确定要做的事情。这就是程序流程控制。

根据明天天气情况,打不打游戏的事情,文字描述的伪代码可以这样写:

如果(明天下雨)
	{
	在家玩游戏
}

 如果(if)就是判断条件的开始。

括号中的“明天下雨”表示明天天气情况,在编程中当然不能直接写上天气情况,要用程序语言来描述。可以使用真(true)或假(false)表示。真假也是C#语言的一种数据类型。

大括号的内容即条件为真时执行的语句。所谓的条件为真或假就是下不下雨的问题。

C#语言的代码如下:

using System;

namespace ConsoleApp19
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("*************************");
            bool tomorrowWeather;  //声明一个布尔类型的变量,表示明天天气情况。
                                    //明天下雨为真(true),不下雨为假(false)。
            tomorrowWeather = true;

            if (tomorrowWeather==true)
            {
                Console.WriteLine("在假打游戏");
            }
            Console.WriteLine("*************************");
        }
    }
}

第10行中的语句表示声明一个布尔类型的变量,变量名称为tomorrowWeather,变量的值可能为真也可能为假。
第12行将明天的天气情况设定为下雨(true),即将真值赋给tomorrowWeather。
第14行判断代码开始,用if表示开始,之后跟小括号内的判断内容,判断明天天气情况,括号里的判断内容结果为真就执行后面大括号内的代码,否则跳过,执行大括号后吗的代码。其中的判断符号为“==”两个等号。一个等号是赋值,两个等号是判断。

运行结果:

 如果明天不下雨,大括号里的语句就不执行。修改天气为不下雨(false)。

using System;

namespace ConsoleApp19
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("*************************");
            bool tomorrowWeather;  //声明一个布尔类型的变量,表示明天天气情况。
                                    //明天下雨为真(true),不下雨为假(false)。
            tomorrowWeather = false;

            if (tomorrowWeather==true)
            {
                Console.WriteLine("在假打游戏");
            }
            Console.WriteLine("*************************");
        }
    }
}

 运行结果:

刚刚没想好不下雨去干啥,现在想好了,如果明天不下雨就去公园。那么就可以再写一个if判断语句。

using System;

namespace ConsoleApp19
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("*************************");
            bool tomorrowWeather;  //声明一个布尔类型的变量,表示明天天气情况。
                                    //明天下雨为真(true),不下雨为假(false)。
            tomorrowWeather = false;

            if (tomorrowWeather==true)
            {
                Console.WriteLine("明天下雨,在家打游戏");
            }

            if (tomorrowWeather == false)
            {
                Console.WriteLine("明天不下雨,去公园玩");
            }
            Console.WriteLine("*************************");

        }
    }
}

 运行结果:

两个if,根据 tomorrowWeather的结果执行不同的语句。

  
例题:编写一个程序,输入星期数字,给出星期的汉字。
思路:声明一个变量保存输入的数字,然后使用if判断,输入1,则输出星期一,输入2则输出星期二。
代码:
using System;

namespace Week
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("*************************");
            //声明变量
            string weekNumber=Console.ReadLine();
            //判断输出
            if (weekNumber == "1") { Console.WriteLine("星期一"); }
            if (weekNumber == "2") { Console.WriteLine("星期二"); }
            if (weekNumber == "3") { Console.WriteLine("星期三"); }
            if (weekNumber == "4") { Console.WriteLine("星期四"); }
            if (weekNumber == "5") { Console.WriteLine("星期五"); }
            if (weekNumber == "6") { Console.WriteLine("星期六"); }
            if (weekNumber == "7") { Console.WriteLine("星期日"); }
            //判断不符合要求的输入
            if (weekNumber == null) { Console.WriteLine("你什么都没有输入"); }

            Console.WriteLine("*************************");

        }
    }
}

运行结果:

 

 2.WPF中的RadioButton控件
RadioButton 是单选控件,为用户提供由两个或多个互斥选项组成的选项集。所谓互斥,可以理解为不能同时存在。如性别:新生儿不是男的必然就是女的。
新建一个WPF项目,解决方案命名为StudentGender。

将工具箱中的Label控件拖放到WPF设计器的空白区域。选中WPF设计器中Label控件,将其名称修改为OutputLabel。

将工具箱中的Button控件拖放到WPF设计器的空白区域。选中WPF设计器中Button控件,将其Content属性修改为确定。

将工具箱中的RadioButton控件拖放到WPF设计器的空白区域。拖放2个,分别将其名称修改为RadioButtonMale和RadioButtonFemale,将其content属性修改为男、女。
控件的大小和对齐可以适当调整。

双击Button按钮,再Button_Click()中输入代码。

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 判断RadioButton是否被选中,选中了就提示选中的RadioButton
            // (bool)表示强制转换数据类型,RadioButtonMale.IsChecked表示是否被选中
            if ((bool)RadioButtonMale.IsChecked)     { OutputLabel.Content = "选择了性别:男"; }
            if((bool)RadioButtonFemale.IsChecked)    { OutputLabel.Content = "选择了性别:女"; }
        }

 运行个结果:

 

-----------------------------------------------------
使用WPF再做一遍上面的例题。
新建一个WPF项目,解决方案命名为WPFWeek。

将工具箱中的Label控件拖放到WPF设计器的空白区域。选中WPF设计器中Label控件,将其名称修改为OutputLabel。

将工具箱中的Button控件拖放到WPF设计器的空白区域。选中WPF设计器中Button控件,将其Content属性修改为确定。

将工具箱中的RadioButton控件拖放到WPF设计器的空白区域。拖放7个,分别将其名称修改为RadioButtonWeek1-7,将其content属性修改为1-7。
控件的大小和对齐可以适当调整。

双击Button按钮,再Button_Click()中输入代码。

private void Button_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)RadioButtonWeek1.IsChecked) { OutputLabel.Content = "星期一"; }
            if ((bool)RadioButtonWeek2.IsChecked) { OutputLabel.Content = "星期二"; }
            if ((bool)RadioButtonWeek3.IsChecked) { OutputLabel.Content = "星期三"; }
            if ((bool)RadioButtonWeek4.IsChecked) { OutputLabel.Content = "星期四"; }
            if ((bool)RadioButtonWeek5.IsChecked) { OutputLabel.Content = "星期五"; }
            if ((bool)RadioButtonWeek6.IsChecked) { OutputLabel.Content = "星期六"; }
            if ((bool)RadioButtonWeek7.IsChecked) { OutputLabel.Content = "星期七"; }
        }

 运行结果:

 

 参考资料
MSDN:
1.RadioButton
https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.controls.radiobutton?view=net-6.0
2.if
https://www.runoob.com/csharp/csharp-if.html
3.bool
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/bool
 4.比较运算符
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/comparison-operators
 
--EOF--
posted @ 2021-09-19 15:11  冲浪的奶糖  阅读(355)  评论(0编辑  收藏  举报