• C#上手练习1(if语句、Swich语句)


    1、打印字符串。

    2、调用简单方法,方法里有if语句、Swich语句。

    C# if else 语句是最常用的条件语句,并且 if else 语句的形式有多种,包括单一条件的 if 语句、二选一条件的 if else 语句以及多选一条件的 if else if 语句。下面将详细介绍这 3 种形式。

    单一条件的 if 语句

    单一条件的 if 语句是最简单的 if 语句,只有满足 if 语句中的条件才能执行相应的语句。

    具体的语法形式如下。

    if(布尔表达式)
    {
        语句块;
    }

    这里语句块是指多条语句。当布尔表达式中的值为 True 时执行语句块中的内容,否则不执行。

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    C# switch case 语句也是条件语句的一种,与上一节介绍的《C# if else语句》是类似的,但在判断条件的选择上会有一些局限性。

    具体的语法形式如下。

    switch(表达式)
    {
        case 值 1:
            语句块 1;
            break;
        case 值 2:
            语句块 2;
            break;
            ...
        default:
            语句块 n;
            break;
    }

    在这里,switch 语句中表达式的结果必须是整型、字符串类型、字符型、布尔型等数据类型。

    如果 switch 语句中表达式的值与 case 后面的值相同,则执行相应的 case 后面的语句块。

    如果所有的 case 语句与 switch 语句表达式的值都不相同,则执行 default 语句后面的值。

    default 语句是可以省略的。需要注意的是,case 语句后面的值是不能重复的。

    using KingTest01;
    using System;
    
    namespace KingTest01
    
    {
        class Program1
        {
            static void Main(string[] args)
            {
                Console.WriteLine("1234的千分位是" + 1234 / 1000);
                Console.WriteLine("1234的百分位是" + 1234 / 100 % 10);//12.34
                Console.WriteLine("1234的十分位是" + 1234 / 10 % 10);//123.4
                Console.WriteLine("1234的个分位是" + 1234 % 10);
                Console.WriteLine("2的10次方是" + (2 << 10));
    
                Program1 Test = new Program1();
                Test.year(2020);
                Test.calculate(11);
                Test.calculate1(10, 20);
                Program1 Write = new Program1();
                Write.Write();
    
            }
    
            public void year(int a)
            {
                if (a % 4 != 0)
                {
                    Console.WriteLine(a + "不是闰年");
                }
                else
                {
                    Console.WriteLine(a + "是闰年");
                }
            }
    
            public void calculate(int a)
            {
                Console.WriteLine(a + "" + (a % 2 == 0 ? "偶数" : "奇数"));
    
            }
            public void calculate1(int a, int b)
            {
                Console.WriteLine(a + "" + b + "较大的是" + (a > b ? a : b));
    
            }
            public void Write()
            {
                Console.WriteLine("请输入您的得分情况");
    
                int a = int.Parse(Console.ReadLine());//将屏幕输入的字符串转换为int类型
                switch (a / 10)
                {
                    case 10:
                        Console.WriteLine("您的得分是" + a + ";非常优秀");
                        break;
                    case 9:
                        Console.WriteLine("您的得分是" + a + ";优秀");
                        break;
                    case 8:
                        Console.WriteLine("您的得分是" + a + ";良好");
                        break;
                    case 7:
                        Console.WriteLine("您的得分是" + a + ";不错");
                        break;
                    case 6:
                        Console.WriteLine("您的得分是" + a + ";一般");
                        break;
                    default:
                        Console.WriteLine("您的得分是" + a + ";非常糟糕");
                        break;
    
                }
    
            }
        }
    }
  • 相关阅读:
    [转]Asp.Net 备份和恢复SQL SERVER 数据库
    alert 的封装
    using(sqlConnection conn=new sqlConnection) 中using的作用
    dotnet 上传大文件的配置的方法
    allowDefinition='MachineToApplication'
    转 Server Application Error报错信息的解决方案
    url 自动加入链接
    MVC中使用RadioButtonFor
    linux iptables squid 透明代理
    linux iptables网关配置,端口转发
  • 原文地址:https://www.cnblogs.com/BruceKing/p/11542067.html
Copyright © 2020-2023  润新知