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)迭代部分: ...
随机推荐
- Kafka基本知识回顾及复制
Producers发布记录到集群,集群维护这些记录并且将记录分发给Consumers. 在Kafka中,最关键的抽象是topic.Producers发布记录到一个topic,Consumers订阅一个 ...
- 使用performance进行网页性能监控
由于项目需要, 需要对网页的一些性能进行监控, 接触到了performance, window.performance 提供了一组精确的数据,经过简单的计算就能得出一些网页性能数据, 将这些数据存储为 ...
- PHP的 first day of 和 last day of
话不多说,先上代码(当前是2017年6月2日) echo date("Y-m-d", strtotime("2017-02 first day of")).'& ...
- 织梦autoindex应用 dedecms循环中判断第几条数据
arclist 标签下使用 [field:global.autoindex/] 默认从1开始 {dede:arclist row='10' titlelen='48' typeid='1' chann ...
- Array 数组的排序 sort
JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序.sort() 方法用于对数组的元素进行排序.语法如下:arrayObject.sort(s ...
- 某控股公司OA系统ORACLE DG搭建
*此处安装ORACLE DATAGUARD是利用ORACLE RMAN DUPLICATE方式安装.*可以搭建好ORACLE DG再来impdp生产数据,也可以先导入主库数据再来做DG*注意看下面的配 ...
- ASP.NET根据当前时间获取,本周,本月,本季度等时间段
DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek. ...
- ch11 持有对象
Java集合的基本类型:List.Set.Queue.Map 使用容器时若未指定泛型参数ArrayList apples=new ArrayList();,则容器中所有元素都为Object类型,使用时 ...
- 关于springMVC中component-scan的问题以及springmvc.xml整理
关于springMVC中component-scan的问题以及springmvc.xml整理 一.component-scan问题和解决办法 最近在学习使用springMVC+myba ...
- the c programing language 学习过程2
manipulated 操纵 notations符号 hexadecimal十六进制 precision精度 be concatenated at 把····联系起来 enumerations枚举 ...