C#上手练习4(Break、CONITINUE语句)
2024-09-03 14:38:18
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 = ; i < ; i++)
{
if (i%!=) {
continue;//当i的余数不等于0的时候,就跳出循环执行下一次循环。
}
Console.WriteLine(i);
}
}
}
}
猜数字例子:
using System;
namespace _break
{
class class1
{
static void Main()
{
int input, randomNum, countfirst, countend;
bool exit = false;
countend = ;
countfirst = ; do
{
Random rand = new Random();
randomNum = rand.Next(, );
Console.WriteLine("你有5次机会猜数字,请输入1-100的整数,剩余次数为{0}", countend - countfirst);
try
{
input = Convert.ToInt32(Console.ReadLine());
if (input < | input > )
{
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);
}
}
}
最新文章
- 8.dns服务的搭建
- View绘制机制
- [Android]Android开发入门之HelloWorld
- ldap + kerberos + google authentication 实现两步验证
- 原生js事件委托
- LeetCode:Maximum Depth of Binary Tree_104
- PAT A 1013. Battle Over Cities (25)【并查集】
- HDU 5919 Sequence II 主席树
- AlwaysON同步性能监控的三板斧
- Solr部署如何启动
- apple iphone 3gs 有锁机 刷机 越狱 解锁 全教程(报错3194,3014,1600,短信发不出去等问题可参考)
- Selenium webdriver 查找元素
- Python:黑板课爬虫闯关第四关
- (转)Microsoft Print to PDF
- python学习笔记之函数的参数
- 设置JVM参数的几种方式解决java.lang.OutOfMemoryError:Java heap space
- Smooth Face Tracking with OpenCV
- (用了map) Registration system
- 记一次线上gc调优的过程
- Windows命令行使用总结(持续更新)
热门文章
- 剑指Offer-42.和为S的两个数字(C++/Java)
- [CodeForces-1225B] TV Subscriptions 【贪心】【尺取法】
- Shape.Type属性名称及对应值列表
- Java描述设计模式(04):抽象工厂模式
- [Spring cloud 一步步实现广告系统] 4. 通用代码模块设计
- 使用Kubernetes进行ProxySQL本机群集
- kali安装openvas
- OpenCV:图像的合并和切分
- 你以为的Jquery选择器是什么样子的?
- 使用 TSPITR 恢复删除的表空间的步骤 (Doc ID 1277795.1)