C#迭代语句、跳转语句--C#基础
1、foreach每执行一次内含的代码时,循环变量就会一次读取集合中的一个元素,不需要个数。循环变量只是一个只读的局部变量,这个值是不能修改的。char后的word是 foreach语句的迭代变量,它是只读的,不允许修改。
源程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 表达式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一串文字:");
String str = Console.ReadLine();
foreach (char item in str)
{
if (char.IsWhiteSpace(item))
{
Console.WriteLine();
}else{
Console.Write(item);
}
}
Console.ReadKey();
}
}
}
2、break跳转语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 表达式
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 11; i++)
{
if (i % 4 == 0)
{
break;
}
Console.Write(i);
}
Console.ReadLine();
}
}
}
3、continue主要用于停止当前的迭代语句,结束本次循环,只能用于迭代语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 表达式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("50以内的奇数:");
for (int i = 1; i <=50; i++)
{
if (i % 2 == 0) {
continue;
}
Console.Write(i+"\t");
}
Console.ReadKey();
}
}
}
4、return语句使用时有两种格式
(1)return;
(2)return 表达式;
return语句只能出现在方法中,当调用方法,回到主函数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 表达式
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入三个整数按回车键确认每个数的输入:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
Console.WriteLine("平均数为{0}", average(a, b, c));
}
Console.ReadLine();
}
static double average(int A,int B,int C) {
return (A + B + C) / 3;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 表达式
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入三个整数按回车键确认每个数的输入:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
average(a,b,c);
}
Console.ReadLine();
}
static void average(int A,int B,int C) {
Console.WriteLine("平均数为{0}", (A+B+C)/3);
return;
}
}
}
5、goto语句使用格式
goto+标识符标识符标志程序位置的方法
作用:当程序执行到goto语句时,程序会跳转到标识符所标识符所标志的位置
goto语句使用会使代码的易读性下降,在编写程序的时候尽量少用goto语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 表达式
{
class Program
{
static void Main(string[] args)
{
//5的阶层等于?
Console.WriteLine("5的阶乘等于?根据以下选项选择正确答案,回车键确认!");
Console.WriteLine("1. 5!=5\n2. 5!=10\n3. 5!=20\n4. 5!=60");
//用户的判定
//标识符标志内容
error:
{
Console.WriteLine("您回答错了!");
}
int option=int.Parse(Console.ReadLine());
switch(option){
case 1:
case 2:
case 3: goto error;
case 4: goto right;
default :
Console.WriteLine("选项不存在,请重新选择");
//break;
goto end;
}
right:
{
Console.WriteLine("恭喜答对了");
}
end: ;
//end: { }
Console.ReadLine();
}
}
}
C#迭代语句、跳转语句--C#基础的更多相关文章
- 第一天:javascript实现界面运算及循环语句跳转语句
文档位置:untitled3(c:\user\dell\WebstormProjects\untitled3\testjstry0.html) 知识点1: 1.新创建html文件,编辑文档如下: &l ...
- 【2-24】for循环嵌套,跳转语句,异常语句,穷举法、迭代法
For循环嵌套与if嵌套相似,是在for中再套for,其结构如下: For(;;) { For(;;){} }经典题型为打印星星例: Console.Write("请输入一个奇数:" ...
- 【2017-02-24】循环嵌套、跳转语句、异常语句、迭代穷举、while
一.循环嵌套 1.格式 for() { for() { } } 2.执行顺序 先执行外边循环进到循环体发现里面的循环,开始执行里面的循环.等到里面的循环执行完毕,再执行外边的循环. 在外面循环第一次, ...
- for循环的表达规则,for循环的嵌套,跳转语句;穷举;迭代;异常处理
for循环的基本格式 for(表达式1:表达式2:表达式3) { 循环体: } for循环的四要素 表达式1就是变量初始化:表达式2就是循环条件:表达式3是状态改变 static void Main( ...
- 【2017-2-24】C#循环嵌套,跳转语句,迭代穷举,异常语句,while循环
循环嵌套 在一个循环体语句中包含另一个循环语句: 99乘法表 ; i <= ; i++) { ; j <= i; j++) { Console.Write(i+"x"+ ...
- c#循环语句 for 循环嵌套的练习。还有跳转语句,异常语句,迭代穷举介绍
先说一下循环嵌套:循环嵌套就是再一个循环里面再放一个循环,也就是说如果没一个循环都循环10次,那么第一个循环是1的时候,嵌套的循环会循环十次.也就是10*10的效果. for 循环语句 主要还是逻辑思 ...
- javascript语句——条件语句、循环语句和跳转语句
× 目录 [1]条件语句 [2]循环语句 [3]跳转语句 前面的话 默认情况下,javascript解释器依照语句的编写顺序依次执行.而javascript中的很多语句可以改变语句的默认执行顺序.本文 ...
- C#语句2——循环语句(for穷举、迭代和while循环)
一.for循环拥有两类: (一).穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元. ...
- 房上的猫:for循环,跳转语句与循环结构,跳转语句进阶
一.for循环 1.定义: for循环语句的主要作用是反复执行一段代码,直到满足一定条件为止 2.组成部分: (1)初始部分:设置循环的初始状态 (2)循环体:重复执行的代码 (3)迭代部分: ...
随机推荐
- [原]开源的视频转换器,支持gpu,绝对好用ffmpeg的GUI==》dmMediaConverter最新版本2.3
dmMediaConverter is a crossplatform FFmpeg frontend (GUI) exposing some of its features. It is inten ...
- AOF持久化
AOF持久化是通话记录执行的命令进行存储的 AOF持久化的事先可以分为 (1)文件追加,新写入的命令都会被追加到redisserver的aof_buf缓冲区里面. 在服务器每一次执行事件循环的时候都会 ...
- 菜鸟的it之路-起航
之前在知乎上看见怎么学习数据结构下一位答主的回答,他引用了N.Wirth(沃斯)的话:程序=数据结构+算法.(哈,菜鸟无法验证这句话的正确性有多大)但毫无疑问的是,数据结构应当是一名菜鸟程序狗要重点学 ...
- 一、scrapy的下载安装---Windows(安装软件太让我伤心了)
写博客就和笔记一样真的很有用,你可以随时的翻阅.爬虫的爬虫原理与数据抓取.非结构化与结构化数据提取.动态HTML处理和简单的图像识别已经学完,就差整理博客了 开始学习scrapy了,所以重新建了个分类 ...
- PHP 变量的实现原理
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica } p.p2 { margin: 0.0px 0.0px 0.0px 0. ...
- .NET常用第三方库(包)总结
文章会不定期更新,以下内容均为个人总结,欢迎各位拍砖指正 序列化与反序列化 JSON.NET应该是.NET平台上使用最为广泛的序列化/反序列化包了,ASP.NET和ASP.NET Core中默认序列化 ...
- Java SocketChannel 读取ByteBuffer字节的处理模型
在JAVA中的流分为字节流或字符流,一般来说采用字符流处理起来更加方便.字节流处理起来相对麻烦,SocketChannel中将数据读取到ByteBuffer中,如何取出完整的一行数据(使用CRLF分隔 ...
- acdrem1083 人民城管爱人民 DP
思路:d(i, 0)表示从节点i到达大运村的最短路径,d(i, 1)表示从节点i到达大运村的次短路径. 1.最短路:当做DAG处理即可. 2.次短路:假设当前在u点处,下一个节点是v.v到终点的最短路 ...
- clearstatcache
clearstatcache clearstatcache — 清除文件状态缓存 void clearstatcache ([ bool $clear_realpath_cache = false [ ...
- 安装linux环境及相关包方法
安装linux系统: 先下载 iso镜像文件,在windows系统下,插入u盘,下载 Ultraiso软件,安装好后,根据 http://jingyan.baidu.com/article/a378c ...