C#基础之流程控制(练习)

1.switc-case练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _switch_case
{
    class Program
    {
        static void Main(string[] args)
        {
           
                     // 成绩>=90 :A      
                     //90>成绩>=80 :B 	
                     //80>成绩>=70 :C
                     //70>成绩>=60 :D
                     //         成绩<60  :E

            Console.WriteLine("请输入一个考试成绩");
            int score = Convert.ToInt32(Console.ReadLine()); //0-100

            switch (score/10)//你要把范围 变成一个定值
            {
                case 10: //case 10要执行的代码跟case 9是一样的
                case 9: Console.WriteLine("A");
                    break;
                case 8: Console.WriteLine("B");
                    break;
                case 7: Console.WriteLine("C");
                    break;
                case 6: Console.WriteLine("D");
                    break;
                default: Console.WriteLine("E");
                    break;
            }
            Console.ReadKey();



        }
    }
}

2.闰年练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _year
{
    class Program
    {
        static void Main(string[] args)
        {
            //请用户输年份,再输入月份,输出该月的天数.(结合之前如何判断闰年来做)
            Console.WriteLine("请输入一个年份");
            try
            {
                int year = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入一个月份");
                try
                {
                    int month = Convert.ToInt32(Console.ReadLine());//1-12
                    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:
                                //由于2月 有平年和闰年的不同  所以首先要判断一下当年是不是闰年
                                if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                                {
                                    day = 29;
                                }
                                else
                                {
                                    day = 28;
                                }
                                break;
                            //4 6  9 11
                            default: day = 30;
                                break;
                        }

                        Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
                    }//if判断的括号
                    else
                    {
                        Console.WriteLine("输入的月份不符合要求,程序退出");
                    }
                }//try月份括号
                catch//跟月份配对
                {
                    Console.WriteLine("输入的月份有误,程序退出");
                }
            }//try年份的括号
            catch//跟年份的try配对
            {
                Console.WriteLine("输入的年份有误,程序退出");
            }
            Console.ReadKey();
        }
    }
}

3.while练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _while
{
    class Program
    {
        static void Main(string[] args)
        {
            //输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩
            //10 
            //循环体:提示输入学员成绩,接收,并转换成整数类型,累加到总成绩当中
            //循环条件:循环的次数小于等于班级人数

            //Console.WriteLine("请输入班级人数");
            //int count = Convert.ToInt32(Console.ReadLine());
            //int sum = 0;//用来存放总成绩
            //int i=1;//声明一个循环变量用来记录循环的次数
            //while (i <= count)
            //{
            //    Console.WriteLine("请输入第{0}个学员的考试成绩",i);
            //    int score = Convert.ToInt32(Console.ReadLine());
            //    //表示把每一个学员的成绩累加到总成绩当中
            //    sum += score;
            //    i++;
            //}
            //Console.WriteLine("{0}个人的班级总成绩是{1}平均成绩是{2}",count,sum,sum/count);
            //Console.ReadKey();

          

            //2006年培养学员80000人,每年增长25%,
            //请问按此增长速度,到哪一年培训学员人数将达到20万人?

            //循环体:人数每年增长25%
            //循环条件:人数>=20万

            //double people = 80000;
            //int year = 2006;
            //while (people < 200000)
            //{
            //    people *= 1.25;
            //    year++;
            //}

            //Console.WriteLine("到第{0}年的时候人数将达到20万人",year);

            //Console.ReadKey();



            //提示用户输入yes或者no
            //要求:只能输入yes或者no,只要不是yes或者no就要求用户一直重新输入

            //循环体:提示用户输入 我们接收并且判断
            //循环条件:输入的不能是yes或者no

            //string input = "";//yes
            //while (input != "yes"&& input != "no")
            //{
            //    Console.WriteLine("请输入yes或者no");
            //    input = Console.ReadLine();
            //}



            //提示用户输入用户名和密码 要求用户名等于admin密码等于888888
            //只要用户名错误或者密码错误就重新输入
            //但是,最多只能输入3次

            //循环体:提示用户输入用户名和密码  接收  判断
            //循环条件:用户名或者密码错误  最多错误3次

            //int i = 1;
            //string userName = "";
            //string userPwd = "";
            //while ((userName != "admin" || userPwd != "888888") && i <= 3)
            //{
            //    Console.WriteLine("请输入用户名");
            //    userName = Console.ReadLine();
            //    Console.WriteLine("请输入密码");
            //    userPwd = Console.ReadLine();
            //    i++;
            //}
            //Console.ReadKey();


            //写两个循环
            //第一个循环提示用户A输入用户名 要求A的用户名不能为空,只要为空,就要求A一直重新输入

            //循环体:提示A输入用户名 接收  判断
            //循环条件:用户名为空

            string userNameA = "";
            while (userNameA == "")
            {
                Console.WriteLine("请输入用户名,不能为空");
                userNameA = Console.ReadLine();
            }
            //  Console.ReadKey();



            //第二个循环提示用户B输入用户名,要求B的用户名不能跟A的用户名相同 并且不能为空
            //只要为空,并且跟A的用户名相同,就一直提示用户B重新输入用户名


            //循环体:提示输入B的用户名 接收判断
            //循环条件:用户名为空 或者跟A的相同

            Console.WriteLine("请输入用户名,不能跟A相同,并且不能为空");
            string userNameB = Console.ReadLine();
            while (userNameB == "" || userNameB == userNameA)
            {
                if (userNameB == "")
                {
                    Console.WriteLine("用户名不能为空,请重新输入");
                    userNameB = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("用户名B不能跟A的用户名相同,请重新输入");
                    userNameB = Console.ReadLine();
                }
            }

            Console.ReadKey();





        }
    }
}

4.do-while循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _do_while
{
    class Program
    {
        static void Main(string[] args)
        {
            
            //要求用户输入用户名和密码,只要不是admin、888888就一直提示用户名或密码错误,请重新输入
            //string name = "";
            //string pwd = "";
            //do
            //{
            //    Console.WriteLine("请输入用户名");
            //    name = Console.ReadLine();
            //    Console.WriteLine("请输入密码");
            //    pwd = Console.ReadLine();
            //} while (name != "admin" || pwd != "888888");
            //Console.WriteLine("登陆成功");
            //Console.ReadKey();


            //练习2:不断要求用户输入学生姓名,输入q结束.

            string name = "";
            while (name != "q")
            {
                Console.WriteLine("请输入学员姓名,输入q结束");
                name = Console.ReadLine();
            }
            Console.ReadKey();

            //do
            //{
            //    Console.WriteLine("请输入学员姓名,输入q结束");
            //    name = Console.ReadLine();
            
            //}while(name!="q");
            //Console.ReadKey();


        }
    }
}

5.for循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01for
{
    class Program
    {
        static void Main(string[] args)
        {
            //求1-100之间的所有整数和   偶数和  奇数和
            //int sum = 0;
            //int n = 100;
            //for (int i = 1; i <= n; i += 2)
            //{
            //    sum += i;
            //}
            //Console.WriteLine(sum);
            //Console.ReadKey();

            //找出100-999间的水仙花数?
            //水仙花数指的就是 这个百位数字的
            //百位的立方+十位的立方+个位的立方==当前这个百位数字
            //153  1 125  27  153  i
            //百位:153/100
            //十位:153%100/10
            //个位:153%10

            for (int i = 100; i <= 999; i++)
            {
                int bai = i / 100;
                int shi = i % 100 / 10;
                int ge = i % 10;
                if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i)
                {
                    Console.WriteLine("水仙花数有{0}",i);
                }
            }
            Console.ReadKey();

        }
    }
}

6.乘法口诀表打印

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _chengji
{
    class Program
    {
        static void Main(string[] args)
        {
            //for (int i = 1; i <= 9; i++)
            //{
            //    for (int j = 1; j <= i; j++)
            //    {
            //        Console.Write("{0}*{1}={2}\t", i, j, i * j);
            //    }
            //    Console.WriteLine();//换行
            //}
            //Console.ReadKey();

            //Console.Write("Hello Wor\tld");
            //Console.WriteLine();
            //Console.Write("Hello World");
            //Console.ReadKey();

            Console.WriteLine("请输入一个数字");
            int number = Convert.ToInt32(Console.ReadLine());


            for (int i = 0; i <=number; i++)
            {
                Console.WriteLine("{0}+{1}={2}",i,number-i,number);
            }
            Console.ReadKey();
        }
    }
}

7.continue练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _continue
{
    class Program
    {
        static void Main(string[] args)
        {
            //练习1:用 while continue实现计算1到100(含)之间的除了能被7整除之外所有整数的和。

            //int sum = 0;
            //int i=1;
            //while (i <= 100)
            //{
            //    if (i % 7 == 0)
            //    {
            //        i++;
            //        continue;
            //    }
            //    sum += i;
            //    i++;
            //}
            //Console.WriteLine(sum);
            //Console.ReadKey();


            //找出100内所有的素数
            //素数/质数:只能被1和这个数字本身整除的数字
            //2 3  4  5  6  7
            //7   7%1   7%2 7%3 7%4 7%5 7%6  7%7  6%2
           
            for (int i = 2; i <= 100; i++)
            {
                bool b = true;
                for (int j = 2; j <i; j++)
                {
                    //除尽了说明不是质数 也就没有再往下继续取余的必要了
                    if (i % j == 0)
                    {
                        b = false;
                        break;
                    }
                }

                if (b)
                {
                    Console.WriteLine(i);
                }
            }

            Console.ReadKey();
            //6   6%2 6%3
        }
    }
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挑战不可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值