Entering through the bad of the do-while loop (in)

Exercise 1: Betty will perform on the stage tomorrow, the teacher said, and then sing the song tomorrow's show again, if satisfied, Betty can go home or else you need to practice again until the teacher satisfied (yes /.. no)

            //循环体:老师不停提问,小兰不停回答
            //循环条件:老师满意(yes)
            string answer = "";//声明变量在do-while外面 避免判断条件时访问不到

            do
            {
                Console.WriteLine("老师,这遍我唱的你满意了嘛?yes/no");//老师提问
                answer = Console.ReadLine();//小兰回答
            } while (answer == "no");//回答no 继续执行循坏
            Console.WriteLine("OK,你可以回家了");//回答yes 
            Console.ReadKey();            

Exercise 2: continue to require the user to enter a number, and then print that number twice, when a user enters q when the program exits

            //循环体:提示用户输入数字并打印自身的二倍
            //循环条件:输入的内容为不等于q
            string input = "";//声明在do外面 利于后续类型转换以及条件判断 
            do
            {
                Console.WriteLine("请输入一个数字,我们将打印他的二倍");//提示用户输入数字
                input = Console.ReadLine();//接收用户输入
                if (input != "q")//当用户输入不等于q时
                {
                    int number = int.Parse(input);//把用户输入的数字转换为int类型
                    Console.WriteLine(number * 2);//打印输入数字的二倍
                }
            } while (input != "q");//用户输入为q 跳出循坏
            Console.ReadKey();           

Exercise 3: continue to require the user to enter a number (assuming the user input is a positive integer) when the end user inputs a maximum value just entered the digital display

            //循环体:提示用户输入数字并输出最大值
            //循环条件:输入的内容不等于end
            int max = 0;//声明最大值初始为0
            string input = "";
            do
            {
                Console.WriteLine("请输入一个数字,输入end我们将显示最大值");//提示用户输入数字
                input = Console.ReadLine();//接收用户输入

                if (input != "end")//当输入不是end
                {
                    int number = int.Parse(input);//把用户输入的数字转换为int类型
                    if (number > max)//判断输入的数字大小
                    {
                        max = number;//如果输入的数字比max大 就把number赋值给max 
                    }
                }
            } while (input != "end");
            Console.WriteLine(max);//输出用户输入的最大值
            Console.ReadKey();

Exercise 4: requires the user to enter a user name and password, if not 12207568,19991020 has been prompted for a username or password, please try again.

            //循环体:提示用户输入用户名和密码
            //循环条件:用户名不等于12207568、密码不等于19991020
            string userName = "";//声明用户的姓名为空
            string userpwd = "";//声明用户的密码为空

            do
            {
                Console.WriteLine("请输入用户名");//提示输入用户名
                userName = Console.ReadLine();//接收用户输入
                Console.WriteLine("请输入密码");//提示输密码
                userpwd = Console.ReadLine();//接收用户输入

            } while (userName!="12207568"||userpwd!="19991020");//循环的两个条件 不确定逻辑与逻辑或可以代入测试
            Console.WriteLine("登录成功");
            Console.ReadKey();

Note: do-while loop
Syntax:
do
{
loop;
} the while (loop condition);
execution: the program first iteration of the loop do is, after the execution is completed, to judge cycling conditions do-while loop,
if established, proceed to do the loop, if not established, then jump out do-while loop.
Features: First cycle, and then determines, loop executes at least once.

Published 17 original articles · won praise 0 · Views 736

Guess you like

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