循环类型:for、while、foreach

循环四要素:初始条件——>循环条件——>循环体——>状态改变

1、for

格式:

for(初始条件;循环条件;状态改变)

{循环体(break;跳出循环体)}ou给出初始条件,先判断是否满足循环条件,如果不满足条件则跳过for语句,如果满足则进入for语句执行,for语句内的代码执行完毕后,将按照状态改变,改变变量,然后id判断是否符合循环条件,符合则继续执行for语句内的代码,直到变量不符合循环条件则终止循环,或者碰到break;命令,直接跳出当前的for循环。

break在这里是跳出循环的意思。

for可以嵌套。

1、让用户输入一个100以内的数
打印1-100之间所有的数,用户输入的数除外

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 作业题1
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个100以内的数:");
int user = Convert.ToInt32(Console.ReadLine());
for (int i = ; i < ; i++)
{
if (i != user)
Console.WriteLine(i);
} Console.ReadLine();
}
}
}

输出结果:

2、让用户输入一个100以内的数
打印1-这个数之间所有的数的和

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 作业题2
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个100以内的数:");
int user = Convert.ToInt32(Console.ReadLine());
int sum = ;
for (int i = ; i < ; i++)
{
sum += i;
if (i==user)
{
break;
}
}
Console.WriteLine(sum); Console.ReadLine();
}
}
}

输出结果:

3、打印100以内所有的质数/素数,再求和

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 作业题3
{
class Program
{
static void Main(string[] args)
{
int sum = ;
for (int i = ; i <= ; i++)
{
int count = ; for (int j = ; j <= i; j++)
{
if (i % j == )
count++;
}
if (count == )
{
Console.Write(i+",");
sum += i;
} }
Console.WriteLine(sum); Console.ReadLine();
}
}
}

输出结果:

问题分析:变量的定义域搞混乱了。把int count=0;定义在for循环外面了。

4、使用一个for循环,分别打印出来100以内的奇数和偶数,分别求和
奇数:1,3,5,7.....
偶数:2,4,6,8.....
奇数和:xxx
偶数和:xxx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 作业题4
{
class Program
{
static void Main(string[] args)
{
int sum1 = ;
int sum2 = ;
string jishu="" ;
string oushu="" ; for (int i = ; i < ; i++)
{
if (i % == )
{
jishu += i + ",";
sum1 += i;
}
else
{
oushu += i + ",";
sum2+=i;
}
}
Console.WriteLine("奇数"+jishu);
Console.WriteLine("偶数" + oushu);
Console.ReadLine();

输出结果:

问题分析:没有想到将jishu定义为string类型进行无限拼接。

5、猜拳(三局两胜)
请输入您的手势:石头
用户手势:石头 电脑手势:剪刀
用户胜:1 电脑胜:0

请输入您的手势:石头
用户手势:石头 电脑手势:石头
用户胜:1 电脑胜:0

请输入您的手势:石头
用户手势:石头 电脑手势:包袱
用户胜:1 电脑胜:1

请输入您的手势:石头
用户手势:石头 电脑手势:剪刀
用户胜:2 电脑胜:1
用户胜利!!!

 

c#基础语句——循环语句(for、while、foreach)的更多相关文章

  1. VBS基础篇 - 循环语句(3) - For...Next

    VBS基础篇 - 循环语句(3) - For...Next   指定循环次数,使用计数器重复运行语句,语法结构如下: 1 2 3 4 5 For counter = start To end [Ste ...

  2. VBS基础篇 - 循环语句(4) - For Each...Next

    VBS基础篇 - 循环语句(4) - For Each...Next   For Each...Next 循环与 For...Next 循环类似.For Each...Next 不是将语句运行指定的次 ...

  3. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

  4. 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】

    1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...

  5. java基础3 循环语句:While 循环语句、do while 循环语句、 for 循环语句 和 break、continue关键字

    一.While循环语句 1.格式 while(条件表达式){ 执行语句: } 2.要点 1,先判断后执行 2,循环次数不定 3,避免死循环 3.举例 题目1:输出0-100之间的所有数 class D ...

  6. go基础语法-循环语句

    1.基础定义 for语句的条件不需要括号(同if语句) ,golang里的循环只有for,没有while sum := 0 for i=0;i<100;i++ { sum += i } 2.条件 ...

  7. Java基础之循环语句、条件语句、switch case 语句

    Java 循环结构 - for, while 及 do...while 顺序结构的程序语句只能被执行一次.如果您想要同样的操作执行多次,,就需要使用循环结构. Java中有三种主要的循环结构: whi ...

  8. python基础之循环语句,格式化输出以及编码

    1.while循环语句 1.1 常见的几种结构    1. while+判断条件 循环体 2. while+判断条件 循环体 else 语句 tips:while循环如果满足条件的话,会一直循环循环体 ...

  9. java基础 流程控制和条件语句,循环语句

    顺序结构 程序的顺序结构: 如果代码里没有流程控制,程序是按照书写的格式从上而下一行一行执行的, 一条语句执行完之后继续执行下一条语句,中间没有判断和跳转,直到程序的结束. if语句 if语句使用bo ...

  10. python基础之循环语句

    一.if条件语句: 语法: 1.if单分支(单重条件判断) if expression: expr_true_suite 注释:expession为真执行代码expr_true_suite if单分支 ...

随机推荐

  1. HTML5学习笔记四:html5结构

    一.大纲:大纲即文档中各内容区块的结构编排 1. 显示编排内容区块:使用section等元素创建文档结构,每个内容区块使用标题(h1~h6,hgroup); 2. 隐式编排内容区块:根据页面所书写的各 ...

  2. cpu95%,查找问题sql

    收到一封告警邮件: Load average on xxx_server reached critical threshold values - 169.5 Current Load Avg = 16 ...

  3. Arcgis js API使用wmts方式加载GeoWebCache中的切片地图(转载)

    使用GeoWebCache的主要目的是其独立安装版能够发布arcgis的切片.我们知道,使用openlayer是调用geoserver最方便的方法,那么在发布完arcgis的切片后,怎么用arcgis ...

  4. Nginx 限流

    原文链接:http://colobu.com/2015/10/26/nginx-limit-modules/?utm_source=tuicool&utm_medium=referral 电商 ...

  5. swift 启动图片的设置

    1 .找到Assets.xcassets 2. 在Assets.xcassets里创建 New LaunchImage 拖入相应的图片 3.选中你的项目,点击General 在App Icons an ...

  6. android延迟执行

     延迟执行可以通过以下操作实现,按照推荐的顺序列出来 1. new Handler().postDelayed(new Runnable(){ public void run() { //execut ...

  7. 理解Window和WindowManager

    Window表示一个窗口的概念,Window是一个抽象类,它的具体实现是PhoneWindow.创建一个Window,需要通过WindowManager即可完成,WindowManager是外界访问W ...

  8. 使用FileReader实现前端图片预览

    在FileReader出现之前,前端的图片预览是这样实现的:把本地图片上传到服务器,服务器把图片地址返回,并把它替换到图片元素的src属性. 这种方法的缺点是:必须要先把图片上传到服务器.那么问题来了 ...

  9. [TensorFlow] Basic Usage

    Install TensorFlow on mac Install pip # Mac OS X $ sudo easy_install pip $ sudo easy_install --upgra ...

  10. asp.net权限认证:Forms认证

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...