首页 > 编程语言 >C# while循环与do循环

C# while循环与do循环

时间:2024-08-28 15:51:18浏览次数:6  
标签:do Console C# sum System int 循环 WriteLine using

学习循环语句之前,先学习跳转语句

continue语句:跳出当前循环,开始一次新的循环,并没有结束循环

break语句:立刻结束循环


while循环语句

while循环语句可以一次都不执行循环体

举例:制作一个小游戏,输入两个和为100的数,积一分,否则游戏结束

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace StatementsExample3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int score = 0;
            bool canContinue = true;
            while (canContinue)
            {
                Console.WriteLine("please input first numble.");
                string str1= Console.ReadLine();
                int x=int.Parse(str1);
                Console.WriteLine("please input first numble.");
                string str2 = Console.ReadLine();
                int y = int.Parse(str2);
                int sum = x + y;
                if (sum == 100)
                {
                    score++;
                    Console.WriteLine("Corret!{0}+{1}={2}",x,y,sum);
                }
                else 
                {
                    Console.WriteLine("Error!{0}+{1}={2}",x,y,sum);
                    canContinue = false;
                }
            }
            Console.WriteLine("Your score is {0}",score);
            Console.ReadLine();
        }
    }
}

do 语句

无论如何都会执行一次循环体

仔细分析上面的代码,无论如何都会执行一次循环体,可以将代码修改如下:

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace StatementsExample3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int score = 0;
            int sum;
            do
            {
                Console.WriteLine("please input first numble.");
                string str1 = Console.ReadLine();
                int x = int.Parse(str1);
                Console.WriteLine("please input first numble.");
                string str2 = Console.ReadLine();
                int y = int.Parse(str2);
                sum = x + y;
                if (sum == 100)
                {
                    score++;
                    Console.WriteLine("Corret!{0}+{1}={2}", x, y, sum);
                }
                else
                {
                    Console.WriteLine("Error!{0}+{1}={2}", x, y, sum);
                   
                }
            } while (sum==100);
            Console.WriteLine("Your score is {0}",score);
            Console.ReadLine();
        }
    }
}

注意:

代码中可能出现错误,输入的数值可能会出现错误,比如输入的不是数字,或者超出了范围,需要使用try语句以及跳转语句。

以及当想主动结束游戏时,输入end结束,需要break语句

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace StatementsExample3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int score = 0;
            int sum=0;
            do
            {
                Console.WriteLine("Please input first numble.");
                string str1 = Console.ReadLine();

                if(str1.ToLower()== "end")
                {
                    break;
                }

                int x = 0;
                try
                {
                    x=int.Parse(str1);//该语句可能会出现错误,比如输入的不是数字,或者超出范围
                }
                catch
                {
                    //当出现错误时,执行以下语句
                    Console.WriteLine("First number have problem!Restart.");
                    continue;//跳出当前循环,并执行一次新的循环
                }
                Console.WriteLine("Please input first numble.");
                string str2 = Console.ReadLine();

                if (str1.ToLower() == "end")
                {
                    break;
                }


                int y = 0;
                try
                {
                    y = int.Parse(str2);
                }
                catch
                {
                    Console.WriteLine("Second number have problem!Restart.");
                    continue;
                }
                sum = x + y;
                if (sum == 100)
                {
                    score++;
                    Console.WriteLine("Corret!{0}+{1}={2}", x, y, sum);
                }
                else
                {
                    Console.WriteLine("Error!{0}+{1}={2}", x, y, sum);
                   
                }
            } while (sum==100);
            Console.WriteLine("Your score is {0}",score);
            Console.ReadLine();
        }
    }
}


 

标签:do,Console,C#,sum,System,int,循环,WriteLine,using
From: https://blog.csdn.net/Freesial_/article/details/140953842

相关文章

  • LCR 018. 验证回文串【简单】
    题目描述给定一个字符串s,验证s是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。本题中,将空字符串定义为有效的回文串。示例1:输入:s=“Aman,aplan,acanal:Panama”输出:true解释:“amanaplanacanalpanama”是回文串示例2:输入:s=“r......
  • [Paper Reading] Transfusion: Predict the Next Token and Diffuse Images with One
    Transfusion:PredicttheNextTokenandDiffuseImageswithOneMulti-ModalModellink时间:24.08机构:Waymo&UniversityofSouthernCaliforniaTL;DR提出一种使用混合模态token来训练transformer,名为transfusion,是一种生成式AI模型。主要工作使用了2T的tokens结合语言......
  • mmcv2.0中build loop、loop.run()、从Dataloader中取数据、run_iter()函数
    本篇博客中,我们以推理为例。首先进入Runer类中的test函数:然后进入Runer类中的build_test_loop函数:然后经过Registry中的build_from_cfg等函数,进入TestLoop类的__init__进行初始化。初始化的时候,会进入父类BaseLoop,在BaseLoop中,会对Dataloader进行build,关于mmcv2.0是如何构建D......
  • 序列化;RPC 【2024年8月28日随笔】
    序列化什么是序列化序列化:把对象转化为可传输的字节序列过程称为序列化反序列化:把字节序列还原为对象的过程称为反序列化为什么序列化序列化机制允许将实现序列化的Java对象转换位字节序列,这些字节序列可以保存在磁盘上,或通过网络传输,以达到以后恢复成原来的对象。序列化机......
  • Effective Java理解笔记系列-第1条-何时考虑用静态工厂方法替代构造器?
    为什么写这系列博客?在阅读《EffectiveJava》这本书时,我发现有许多地方需要仔细认真地慢慢阅读并且在必要时查阅相关资料才能彻底搞懂,相信有些读者在阅读此书时也有类似感受;同时,在解决疑惑的过程中,还存在着有些内容不容易查找、查找到的解答质量不高等问题,于是我决定把我阅读此书......
  • 【阅己书城】docker部署MySQL及Redis
    一、MySQL1拉取mysql镜像dockerpullmysql:5.72启动mysql容器--name指定容器名字-v目录挂载-p指定端口映射-e设置mysql参数-d后台运行dockerrun--namemysql-v/mydata/mysql/data:/var/lib/mysql-v/mydata/mysql/conf:/etc/mysql-v/mydata/mysql/log:/var/......
  • windows下rust中使用ffmpeg
    问题描述想要在rust中使用ffmpeg,首先得安装ffmpeg的开发包,就是include和lib。过程安装ffmpeg官网安装安装llvm官网安装这个我的环境中本来就已经安装了不确定是否真的是必须的。设置环境变量运行cargobuild前设置#powershell中,#D:\dev\ffmpeg-6.1目录下包含了includ......
  • docker pull通过http代理下载镜像
    有时服务器没有外网环境,要pulldocker镜像就没办法了,只能考虑通过代理或离线下载好导入的方式进行,这里记录下通过http代理的方式。一、dockerpull通过http代理下载镜像1.修改/etc/systemd/system/multi-user.target.wants/docker.servicevi/etc/systemd/system/multi-user.ta......
  • openGauss-DCF
    openGauss-DCF可获得性本特性自openGauss2.0.0版本开始引入。特性简介DCF(DistributedConsensusFramework,分布式共识框架)基于Paxos算法实现数据同步强一致。DCF模式开关开启后,DN可以支持基于Paxos协议的复制与仲裁能力。客户价值DN基于Paxos的自选主及日志复制,复制过程中......
  • 兴业证券基于Apache DolphinScheduler的应用实践
    文/兴业证券股份有限公司 刘洋 石良生 柳君 李致琪本文来源于网络,如有侵权,请联系删除任务调度平台,扮演着自动执行预设任务的重要角色,是业务开展过程中不可或缺的一环。随着业务规模的不断扩展,兴业证券每日需要进行数以万计的任务调度,因此,优化和提升任务调度平台的性......