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)迭代部分: ...
随机推荐
- hbuilder ios 打包失败,无法导入p12证书的解决方案
问题描述: 在profile文件和私钥证书通过hbuilder ios 打包成功过的前提下,突然遇到打包失败的问题,问题详情是无法导入p12证书. 探索过程: 本着遇到问题先自省的态度,重复打包了几次 ...
- 新闻热词:从爬虫到react native应用
背景 由于只想了解当天新增的top热词,减少过多信息干扰,打算做一款app实现这个功能. 架构: 热词抓取 -> mysql <=> nodejs <=> nginx & ...
- javamail+ical4j发送会议提醒
本篇讲述小编在使用ical4j时对其的理解与使用,留作笔记的同时希望能帮助到大家! 初学者可以先了解下ical4j的基本信息: iCalender编程基础,了解与使用ical4j:https://ww ...
- nf共享
实验环境是两台Centos6.8 客户端是192.168.3.218 服务端是192.168.3.219 首先配置服务端 1 安装包 用yum安装需要的服务包(两边都安装) yum install n ...
- vim操作备忘录
vim操作备忘录 vim 备忘录 vim的书籍虽然看不不少,可是老是容易忘记,主要是自己操作总结过少,这个博客就主要用来记录一些比较常见的术语和操作,以防止自己再次忘记. <leader> ...
- 一种解决eclipse中安装maven出错的方法
1.安装步骤:https://jingyan.baidu.com/article/a17d5285feb4dd8099c8f26e.html 2.安装第三步的解决办法:m2e 路径换成 http ...
- 初识Vue——模板语法
一.插值 1.文本 数据绑定最常见的形式是使用双大括号({{ }}--"Mustache"语法)的文本插值 <div class="mustache"& ...
- 用Open SSH生成公钥和私钥(Win)
也可以使用 dsa 加密算法进行加密,命令如下: ssh-keygen -t dsa
- 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)
//1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...
- 求指定区间内与n互素的数的个数 容斥原理
题意:给定整数n和r,求区间[1, r]中与n互素的数的个数. 详细见容斥定理 详细代码如下 int solve(int r, int n) { vector<int>p; p.clear ...