C#使用switch多路选择语句判断何为季节

目录

一、 switch语句

二、示例

三、生成


一、 switch语句

        switch语句是多路选择语句,它通过一个表达式的值来使程序从多个分支中选取一个用于执行的分支。

        switch表达式的值只可以是整型、字符串、枚举和布尔类型

        switch语句中多个case可以使用一个break。

        在switch多路选择语句中,多个case标签可以使用一个break关键字,但是只有最后一个case标签中可以带有语句块,而前面的case标签中不可以带有语句块内容。

二、示例

// 使用switch多路选择语句判断季节
namespace _029
{
    public partial class Form1 : Form
    {
        private Label? label1;
        private ComboBox? comboBox1;
        private Button? button1;

        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(12, 16),
                Name = "label1",
                Size = new Size(43, 17),
                TabIndex = 0,
                Text = "选择月份:"
            };
            // 
            // comboBox1
            // 
            comboBox1 = new ComboBox
            {
                FormattingEnabled = true,
                Location = new Point(80, 8),
                Name = "comboBox1",
                Size = new Size(95, 25),
                TabIndex = 1,
                DropDownStyle = ComboBoxStyle.DropDownList  //选择项只读
            };
            comboBox1.Items.AddRange([
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"]);
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(192, 10),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 2,
                Text = "判断季节",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(279, 56);
            Controls.Add(button1);
            Controls.Add(comboBox1);
            Controls.Add(label1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "使用switch多路选择语句判断季节";
            ResumeLayout(false);
            PerformLayout();
        }

        private void Button1_Click(object? sender, EventArgs e)
        {
            switch (//跟据所选月份判断季节
                comboBox1!.SelectedIndex + 1)
            {
                case 3:
                case 4:
                case 5:
                    MessageBox.Show("春季", "提示!");
                    break;
                case 6:
                case 7:
                case 8:
                    MessageBox.Show("夏季", "提示!");
                    break;
                case 9:
                case 10:
                case 11:
                    MessageBox.Show("秋季", "提示!");
                    break;
                case 12:
                case 1:
                case 2:
                    MessageBox.Show("冬季", "提示!");
                    break;
                default://如果没有选择月份弹出提示信息
                    MessageBox.Show("请选择月份", "提示!");
                    break;
            }
        }
    }
}

三、生成

 

相关推荐

  1. c语言:把百分制转化等级制度(switch语句

    2023-12-31 16:10:03       46 阅读
  2. C++入门学习(二十三)选择结构-switch语句

    2023-12-31 16:10:03       59 阅读
  3. C# —— switch语句

    2023-12-31 16:10:03       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-31 16:10:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-31 16:10:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-31 16:10:03       87 阅读
  4. Python语言-面向对象

    2023-12-31 16:10:03       96 阅读

热门阅读

  1. 判断素数的方法大全

    2023-12-31 16:10:03       64 阅读
  2. STL——常用算术生成算法

    2023-12-31 16:10:03       59 阅读
  3. STL——排序算法

    2023-12-31 16:10:03       59 阅读
  4. 【MySQL从入门到精通】常用SQL语句分享

    2023-12-31 16:10:03       73 阅读
  5. 蓝牙常见断开错误码解释

    2023-12-31 16:10:03       200 阅读
  6. V8 基础篇

    2023-12-31 16:10:03       54 阅读