zoukankan      html  css  js  c++  java
  • C#上手练习4(Break、CONITINUE语句)

    C# 中的 continue 语句有点像 break 语句。但它不是强制终止,continue 会跳过当前循环中的代码,强制开始下一次循环。

    对于 for 循环,continue 语句会导致执行条件测试和循环增量部分。对于 while 和 do while 循环,continue 语句会导致程序控制回到条件测试上。

    提示:C# continue 语句必须在循环语句中使用。

    EX.打印出1-20之间的偶数

    using System;
    
    namespace KingTest03
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program program = new Program();
                program.even();
    
            }
           public void even()//输出1-20之间的偶数
            {
                for (int i = 1; i < 20; i++)
                {
                    if (i%2!=0) {
                        continue;//当i的余数不等于0的时候,就跳出循环执行下一次循环。
                    }
                    Console.WriteLine(i);
                }
            }
        }
    }

     猜数字例子:

    using System;
    namespace _break
    {
        class class1
        {
            static void Main()
            {
                int input, randomNum, countfirst, countend;
                bool exit = false;
                countend = 5;
                countfirst = 0;
    
                do
                {
                    Random rand = new Random();
                    randomNum = rand.Next(1, 101);
                    Console.WriteLine("你有5次机会猜数字,请输入1-100的整数,剩余次数为{0}", countend - countfirst);
                    try
                    {
                        input = Convert.ToInt32(Console.ReadLine());
                        if (input < 0 | input > 100)
                        {
                            Console.WriteLine("请输入1-100的整数");
                        }
                        else if (input > randomNum)
                        {
                            Console.WriteLine("随机数是{0},你猜测数字是{1},恭喜你,你大", randomNum, input);
                            countfirst++;
                            if (countfirst > countend)
                            {
                                Console.WriteLine("您已经猜测没有次数了");
                                break;
                            }
    
                        }
                        else if (input < randomNum)
                        {
                            Console.WriteLine("随机数是{0},你猜测数字是{1},恭喜你,你大", randomNum, input);
                            countfirst++;
                            if (countfirst > countend)
                            {
                                Console.WriteLine("您已经猜测没有次数了");
                                break;
                            }
    
                        }
                        else
                        {
                            Console.WriteLine("恭喜你,猜对了");
                            break;
                        }
    
                    }
                    catch (Exception)
                    {
    
                        Console.WriteLine("请输入整数");
    
                    }
                } while (true);
            }
        }
    }

  • 相关阅读:
    热烈祝贺《名博是怎样炼成的》出版
    公司年会在民俗文化村举行
    春节后第一周个人新闻两则
    用asp.net来回收IIS6.0应用程序池
    ComponentArt Web.UI 升级遇到的问题
    今天给博客设计了个博皮
    2009春运购火车票经历
    Android平台下实现一个进程管理器
    【转】Windows平台下Android源码的下载
    【转】 使用git 工具下载android.jar Source Code
  • 原文地址:https://www.cnblogs.com/BruceKing/p/11547247.html
Copyright © 2011-2022 走看看