美文网首页
C#语法基础

C#语法基础

作者: 秦海波 | 来源:发表于2021-11-30 21:41 被阅读0次

    分支语句

    if

    int i = 10;
    if(i == 10)
    {
        Console.WriteLine("i等于10");
    }
    

    if-else

    static void Main(string[] args)
    {
        int i = 10;
        if(i == 20)
        {
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("i不等于20");
        }
    
        if (i < 10)
        {
            Console.WriteLine("i小于10");
        }
        else if (i > 10)
        {
            Console.WriteLine("1>10");
        }
        else
        {
            Console.WriteLine("i=10");
        }
    }
    

    switch

    int i = 3;
                
    switch (i) { 
        case 0:
        Console.WriteLine("男的");
        break;
        case 1:
        Console.WriteLine("女的");
        break ;
        case 2:
        Console.WriteLine("不男不女的");
        break;
        default:
        Console.WriteLine("我可不知道");
        break;
    }
    

    循环

    for

    int i = 10;
    for (int j = 0; j < i; j++)
    {
        Console.WriteLine(j);
    }
    

    增强for

    string[] strings = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    foreach (string s in strings)
    {
        Console.WriteLine(s);
    }
    

    while

    int i=0;
    while (i < 10)
    {
        i++;
        Console.WriteLine(i);
    }
    
    #do-while
    int i=0;
    do
    {
        Console.WriteLine(i++);
    }while (i<10);
    

    相关文章

      网友评论

          本文标题:C#语法基础

          本文链接:https://www.haomeiwen.com/subject/mfnptrtx.html