zoukankan      html  css  js  c++  java
  • .Net基础篇_学习笔记_第五天_流程控制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)
            {
                Console.WriteLine("老师我唱的您满意吗?");
                string answer = Console.ReadLine();
                while (answer == "no")
                {
                    Console.WriteLine("老师我再唱一遍,您满意了吗?");
                    answer = Console.ReadLine();
                }
                Console.ReadKey();
            }
        }
    }

     遇见这种首先执行一边循环体,拿着执行结果然后再去判断是否执行循环,这样的循环,推荐使用do-while循环。

    特点:

    do-while循环:程序会先执行do中的循环体,执行完后,再去判断do-while循环的循环条件,如果成立,继续执行do中的循环体,如果不成立,则跳出do-while循环。(最少执行一遍循环体,侧重于先做一遍,再执行)。

    while循环:先判断再执行,可能一遍也进行循环。

     1 namespace 第六天_do_while循环
     2 {
     3     class Program
     4     {
     5        
     6         static void Main(string[] args)
     7         {
     8             string answer = "";
     9             do
    10             {
    11                 Console.WriteLine("老师,我唱的您满意吗?yes/no");
    12                 answer = Console.ReadLine();
    13 
    14             }while (answer=="no");
    15             Console.WriteLine("OK,可以放学回家了");
    16             Console.ReadKey();
    17         }
    18     }
    19 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 第六天_do_while循环
     8 {
     9     class Program
    10     {
    11        
    12         static void Main(string[] args)
    13         {
    14             string name = "";
    15             while (name!="q")
    16             {
    17                 Console.WriteLine("请输入正确的姓名:");
    18             }
    19             Console.ReadKey();
    20         }
    21     }
    22 }

    转成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)
            {
                string name = "";
                do
                {
                    Console.WriteLine("请输入姓名:");
                    name=Console.ReadLine();
                } while (name!="q");
            }
        }
    }
  • 相关阅读:
    【Css】SCSS基本语法
    【Css】Scss 与 Sass 简单示例
    【移动端】cordova在app中打开外部链接——cordova-plugin-inappbrowser
    border-radius圆角边框属性讲解
    css 设置 transform 无效
    linux下设置php执行命令
    linux下php命令无法使用如何解决
    微信小程序 --- 表单输入验证(手机号、邮箱验证、输入非空)
    微信小程序倒计时组件开发
    小程序--三级联动
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/7209384.html
Copyright © 2011-2022 走看看