Entering through the bad of the while loop (on)

Exercise 1: Print 100 "C # from entry to give up."

            int i = 1;//声明一个变量i记录循环次数
            while (i <= 100)
            {
                Console.WriteLine("C#从入门到放弃第{0}天", i);
                i++;//每循环一次 自身加1  最终使之循环条件不在成立
            }
            Console.ReadKey();

Exercise 2: Enter the number of classes, followed by input student achievement, students calculate the average class score and total score

            int i = 1;//循坏变量记录循坏次数
            int sum = 0;//用来存储学生成绩总和
            Console.WriteLine("请输入班级人数");//提示用户输入班级人数
            try  //用户输入的是数字
            {
                int count = int.Parse(Console.ReadLine());//接收用户输入的班级人数
                while (i <= count)//小于等于输入的班级人数
                {
                    Console.WriteLine("请输入第{0}个人的成绩", i);
                    int score = int.Parse(Console.ReadLine());//接收学员的成绩
                    sum += score;//把每一个学员的成绩累加到sum
                    i++;
                }
                Console.WriteLine("学员为{0}人的平均成绩是{1}总成绩为{2}", count, sum / count, sum);
            }
            catch//用户输入非数字
            {
                Console.WriteLine("你输入的内容不能转换成数字,程序退出");
            }
            Console.ReadKey();

Exercise Three: The teacher asked Xiao Ming, This question is would you do it if Xiao Ming answer "will be the (yes)", you can not do after school if Xiao Ming (no), the teachers say it again, ask whether students?. will do ... until Xiao Ming will only be after school until Xiao Ming will or teacher told him 10 times will not, have school

            string answer = "";//声明小明的回答为空
            int i = 1;//记录循坏次数

            while (answer == "no" || i <= 10)//进入循坏的两个条件
            {
                Console.WriteLine("小明这道题你会了吗yes/no");
                answer = Console.ReadLine();
                if (answer == "yes")//小明回答yes
                {
                    Console.WriteLine("OK,放学回家");
                    break;//跳出循环
                }
                else //回答不为yes
                {
                    if (i == 10)//回答的次数等于10次
                    {
                        Console.WriteLine("勤能补拙是良训,一分辛苦一分才,晚上不要玩手机好好琢磨这道题,必须弄懂");
                    }
                    i++;
                }
            }
            Console.ReadKey();

Exercise Four: training students 80,000 people in 2006, an increase of 25% per year, may I ask Click the growth rate, the number of trainees a year where participants will reach 20 million people?

            int year = 2006;//声明一个整数变量记录初始年份
            double people = 80000;//增长25% ,小数类型这里用double来声明  
            
            while (people<200000)//人数大于20万跳出循坏
            {
                people = people * 1.25;
                year++;//人数每增加一次 年份加1
            }
            
            Console.WriteLine("{0}年培训公司将达到20万人",year);
            Console.ReadKey();

NOTE: while loop introduced
while (loop condition)
{
loop;
}
execution: program proceeds to while the first determined cycle conditions in parentheses while carried is established or if established, i.e. returns a true, then iteration of the loop, the loop after executing once again return to cycling conditions to judge, if still valid, continue the loop, if not true, out of the while loop. Among the while loop, generally there will always be a line of code, can change the loop condition, so that one day no longer holds, if it is not possible to change a line of code loop condition, which is always cycling conditions are true, we call this cycle is called an infinite loop.
The easiest most common infinite loop:
the while (to true)
{

}
Features: first judge, then execute, it is possible not to perform the cycle again.

Published 17 original articles · won praise 0 · Views 737

Guess you like

Origin blog.csdn.net/weixin_44623941/article/details/104732772