前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 笔记之基本语法

C# 笔记之基本语法

作者头像
微软技术分享
发布2022-12-28 17:13:11
2030
发布2022-12-28 17:13:11

标准输入输出:

代码语言:javascript
复制
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello world");
            string Str = Console.ReadLine();
            Console.WriteLine(Str);
        }
    }
}

常用变量类型:

代码语言:javascript
复制
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte s_byte = 254;
            sbyte s_sbyte = 126;
            short s_short = 32766;
            int s_int = 2147483645;
            double s_double = 3.1415926;
            decimal d_decimal = 5000m;
            string s_string = "hello lyshark";
        }
    }
}

if语句:

代码语言:javascript
复制
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const int x = 100;
            const int y = 200;
            int source = 0;
            Console.WriteLine("请输入一个数:");
            source = int.Parse(Console.ReadLine());
            if (source == 0)
            {
                Console.WriteLine("你没有输入任何数.");
            }
            else if (source > x && source < y)
            {
                Console.WriteLine("符合规范");
            }
            Console.ReadKey();
        }
    }
}

switch

代码语言:javascript
复制
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入一个整数: ");
            int score = Convert.ToInt32(Console.ReadLine());
            switch (score / 10)
            {
                case 10:
                case 9:
                    Console.WriteLine("A");break;
                case 8:
                case 7:
                    Console.WriteLine("B");break;

                default:
                    Console.WriteLine("none");break;
            }
            Console.ReadKey();
        }
    }
}

while-dowhile

代码语言:javascript
复制
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] MyArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            int index = 0;
            while (index < 10)
            {
                Console.WriteLine("数组中的值: {0}", MyArray[index]);
                index++;
            }

            index = 0;
            do
            {
                Console.Write("{0} ", MyArray[index]);
                index++;
            } while (index < 10);

            Console.ReadKey();
        }
    }
}

for

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] MyArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            for (int index = 0; index < MyArray.Length; index++)
            {
                Console.WriteLine("下标:{0} --> 数据: {1}", index, MyArray[index]);
            }

            ArrayList alt = new ArrayList();

            alt.Add("你好");
            alt.Add("世界");
            foreach (string Str in alt)
            {
                Console.WriteLine("输出: {0}", Str);
            }

            Console.ReadKey();
        }
    }
}

break

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 0; x < 5; x++)
            {
                for(int y=0; y<10 ;y++)
                {
                    if (y == 3)
                        break;
                    Console.WriteLine("输出: {0}",y);
                }
            }
            Console.ReadKey();
        }
    }
}

goto

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] MyStr = new string[3];

            MyStr[0] = "hello";
            MyStr[1] = "world";
            MyStr[2] = "lyshark";

            for (int x = 0; x < MyStr.Length; x++)
            {
                if(MyStr[x].Equals("lyshark"))
                {
                    goto Is_Found;
                }
            }
        Is_Found:
            Console.Write("查找到成员");
            Console.ReadKey();
        }
    }
}

判断闰年案例:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("输入年份: ");
                int year = Convert.ToInt32(Console.ReadLine());
                Console.Write("输入月份: ");
                int month = Convert.ToInt32(Console.ReadLine());
                if (month >= 1 && month <= 12)
                {
                    int day = 0;
                    switch (month)
                    {
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12: day = 31; break;
                        case 2:
                            if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                                day = 29;
                            else
                                day = 28;
                            break;
                        default: day = 30; break;
                    }
                    Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
                }
            }
            catch
            {
                Console.WriteLine("异常了");
            }
            Console.ReadKey();
        }
    }
}

99口诀表

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 1; x <= 9; x++)
            {
                for (int y = 1; y <= x; y++)
                {
                    Console.Write("{0} * {1} = {2} \t", y, x, x * y);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

随机数产生:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();


            for (int x = 0; x < 100; x++)
            {
                int Num = rand.Next(1, 1024);
                Console.WriteLine("随机数: {0}", Num);
            }
            Console.ReadKey();
        }
    }
}

枚举类型:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    public enum QQState
    {
        OnLine = 1,
        OffLine,
        Leave,
        Busy,
        QMe
    }

    class Program
    {
        static void Main(string[] args)
        {
            QQState state = QQState.OnLine;
            // 将state强转为整数
            int num = (int)state;
            Console.WriteLine(num);

            // 将整数强转为枚举类型
            int num1 = 2;
            QQState x = (QQState)num1;
            Console.WriteLine("状态: {0}",x);

            // 输入一个号兵输出对应状态
            string input = Console.ReadLine();
            switch(input)
            {
                case "1":
                    QQState s1 = (QQState)Enum.Parse(typeof(QQState), input);
                    Console.WriteLine("状态: {0}", s1);
                    break;
            }
            Console.ReadKey();
        }
    }
}

定义结构数据:

代码语言:javascript
复制
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        // 定义结构数据
        public struct Person
        {
            public double width;
            public double height;

            // 结构体支持构造函数
            public Person(double x, double y)
            {
                width = x;
                height = y;
            }
        }

        static void Main(string[] args)
        {
            Person per;

            per.width = 100;
            per.height = 200;
            Console.WriteLine("x = {0} y = {1}", per.width, per.height);

            Person ptr = new Person(10, 20);
            Console.WriteLine("x = {0} y = {1}", ptr.width, ptr.height);

            Console.ReadKey();
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-06-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档