快速阅读程序小技巧和一些写作技巧

本文介绍了C#编程中的一些实用技巧,包括Ctrl+右}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Ctrl + 右 },可以快速在两个花括号之间跳转

2. C W 连续按下两次Tab键,补齐Console.WriteLine();

3. if 连续按下两次Tab键,补齐 if 语句

4.sw 连续按下两次Tab键,补齐 switch 语句

5. 枚举类型写法:写在class同级

enum Level
    {
        High,
        Mid,
        Low
    }

枚举类型调用:

Level mylevel = new Level();
switch (mylevel)
{
    case Level.High:
         Console.WriteLine("High Level");
         break;
    case Level.Mid:
         Console.WriteLine("Mid Level");
         break;
    case Level.Low:
         Console.WriteLine("Low Level");
         break;
    default:
         break;
}

6.while 语句执行0次或多次

int score = 0;
            bool canContinue = true;


            while (canContinue)
            {
                Console.WriteLine("Please input first number:");
                string str1 = Console.ReadLine();
                int x = int.Parse(str1);

                Console.WriteLine("Please input second number:");
                String str2 = Console.ReadLine();
                int y = int.Parse(str2);

                int sum = x + y;
                if(sum == 100)
                {
                    score++;
                    Console.WriteLine("Correct!{0}+{1}={2}",x,y,sum);
                }
                else
                {
                    canContinue = false;
                    Console.WriteLine("Error!{0}+{1}={2}",x,y,sum);
                }

            }
            Console.WriteLine("Your score is {0}",score);
            Console.WriteLine("GAME OVER!");

7.do while 语句,先执行一次do,然后判断while 语句条件是否成立,共执行一次或多次。

           int score = 0;
            int sum = 0;
            do
            {
                Console.WriteLine("Please input first number:");
                string str1 = Console.ReadLine();
                int x = int.Parse(str1);

                Console.WriteLine("Please input second number:");
                String str2 = Console.ReadLine();
                int y = int.Parse(str2);

                sum = x + y;
                if (sum == 100)
                {
                    score++;
                    Console.WriteLine("Correct!{0}+{1}={2}", x, y, sum);
                }
                else
                {
                    Console.WriteLine("Error!{0}+{1}={2}", x, y, sum);
                }

            }while (sum == 100);
            Console.WriteLine("Your score is {0}",score);
            Console.WriteLine("GAME OVER!");

8.continue 语句放弃当前循环,重新开始

break 语句结束循环语句,循环体不再执行,执行循环体之后的语句

9. foreach语句最佳应用场合,对集合进行遍历

int[] myarray = new int[] { 0, 1, 2, 3, 4, 5 };
            //foreach   指月,最佳应用场合对集合进行遍历
            foreach (var current in myarray)
            {
                Console.WriteLine(current);
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值