zoukankan      html  css  js  c++  java
  • C# 笔记之基本语法

    标准输入输出:

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

    常用变量类型:

    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语句:

    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

    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

    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

    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

    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

    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();
            }
        }
    }
    

    判断闰年案例:

    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口诀表

    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} 	", y, x, x * y);
                    }
                    Console.WriteLine();
                }
                Console.ReadKey();
            }
        }
    }
    

    随机数产生:

    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();
            }
        }
    }
    

    枚举类型:

    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();
            }
        }
    }
    

    定义结构数据:

    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();
            }
        }
    }
    

    版权声明: 本博客,文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章【均为原创】作品,转载请务必【添加出处】,您添加出处是我创作的动力!

    警告:如果您恶意转载本人文章,则您的整站文章,将会变为我的原创作品,请相互尊重!
  • 相关阅读:
    Single Number II
    Best Time to Buy and Sell Stock
    Linked List Cycle
    Single Number
    Max Points on a Line
    Strategy
    LRU Cache
    Word Break II
    Text Justification
    Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/LyShark/p/13156473.html
Copyright © 2011-2022 走看看