C#的语法----程序结构(6)

最后这一个循环是我们未来最最常用的for循环,所以篇幅较长,敬请谅解。

我不知道,大家在用while循环的时候,再写控制循环次数的时候,是不是总将i++忘记写,所以while还是有时候不太好用的,

那么,在我们已知循环次数的情况下,我们可以使用for循环来避免。

for循环

语法:

for(表达式1;表达式2;表达式3)

{

循环体;

}

表达式1:声明循环变量,记录循环次数。

表达式2:循环条件。

表达式3:改变循环条件的代码,使之终会不再成立。

我们写一下下面这样一个练习:找出100~999之间的水仙花数(百位3+十位3+个位3=该数)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13
14             for (int i = 100; i <=999; i++)
15             {
16                 int bai = i / 100;
17                 int shi = i % 100 / 10;
18                 int ge = i % 10;
19
20                 if (bai*bai*bai+shi*shi*shi+ge*ge*ge == i)
21                 {
22                     Console.WriteLine("水仙花数有{0}",i);
23                 }
24                 Console.ReadKey();
25             }
26         }
27     }
28 }

练习2:乘法口诀(矩形输出)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             for (int i = 1; i <= 9; i++)
14             {
15                 for (int j = 1; j <= 9; j++)
16                 {
17                     Console.Write("{0}*{1}={2}\t",i,j,i*j);
18                 }
19                 Console.WriteLine();
20             }
21             Console.ReadKey();
22         }
23     }
24 }

练习3:循环录入5人信息并计算平均年龄,如果录入数据出现负数或大于100,立即停止输入并报错

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int sum = 0;
14             bool b = true;
15             for (int i = 0; i < 5; i++)
16             {
17                 Console.WriteLine("请输入第{0}个人的成绩",i+1);
18                 try
19                 {
20                     int age = Convert.ToInt32(Console.ReadLine());
21                     if (age >= 0 && age <= 100)
22                     {
23                         sum += age;
24
25                     }
26                     else
27                     {
28                         Console.WriteLine("输入的年龄有误");
29                         b = false;
30                         break;
31                     }
32                 }
33                 catch
34                 {
35
36                     Console.WriteLine("输入的年龄不正确,程序退出!!!");
37                     b = false;
38                     break;
39                 }
40
41             }
42             if (b)
43             {
44                 Console.WriteLine("5个人的平均年龄是{0}", sum / 5);
45             }
46             Console.ReadKey();
47         }
48     }
49 }


在前几节中,我们介绍了控制循环程序流向的关键字break,下面我们再介绍一个。

continue:立即结束本次循环,判断循环条件,如果成立,则进入下一次循环,否则退出循环。

练习1:用while continue实现实现计算1~100之间的除了能被7整除之外的所有整数的和

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int sum = 0;
14             int i = 1;
15             while (i < 100)
16             {
17                 if (i % 7 == 0)
18                 {
19                     i++;
20                     continue;
21                 }
22                 sum += i;
23                 i++;
24             }
25             Console.WriteLine(sum);
26             Console.ReadKey();
27         }
28     }
29 }

练习2:找出100内的所有质数(只能被1和本身整除的数字)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             for (int i = 2; i <= 100; i++)
14             {
15                 bool b = true;
16                 for (int j = 2; j < i; j++)
17                 {
18                     if (i % j == 0)  //除尽了 不是质数
19                     {
20                         b = false;
21                         break;
22                     }
23                 }
24                 if (b)
25                 {
26                     Console.WriteLine(i);
27                 }
28
29             }
30             Console.ReadKey();
31         }
32     }
33 }


以上1~6节是我们对程序的流程控制的全部介绍,下面我们做一个小引子,引出我们以后一个难点和重点,方法和面向对象。

下面先介绍一个函数:随机数(Random())

步骤如下:

(1)创建能够产生随机数的对象。Random r=new Random();

(2)让产生随机数的的这个对象调用方法产生随机数。int rNumber = r.Next(a,b);



C#的语法----程序结构(6)

原文地址:https://www.cnblogs.com/LiyuLi/p/12093508.html

时间: 2024-08-29 09:42:17

C#的语法----程序结构(6)的相关文章

C#的语法----程序结构(1)

接下来的内容是整个C#学习的脉络,它将各个知识点串联了起来,是整个C#的重点,所以篇幅较长. 首先,我们类比一下PLC和C#执行代码的方式,其实不难发现都是顺序扫描,以Main为程序入口,从上到下一行一行执行.这属于顺序结构.下面我们看看已下几种结构 (1)分支结构:if if-else (2)选择结构:if-else-if  switch-case (3)循环结构:while do-while for foreach 首先先分享分支结构的用法 if语句 语法: if(判断条件) { 要执行的代

C#的语法----程序结构(2)

接下来我们继续学习程序流程控制的语法! switch-case 用来处理多条件的定值的判断. 语法: switch(变量或者表达式的值) { case value1:要执行的代码1: break; case value2:要执行的代码2: break; case value3:要执行的代码3: break; ........ default:要执行的代码4; break; } 执行过程:程序执行到switch处,首先将括号或者表达式的值计算出来,然后拿着这个值一次和case处值进行匹配,一旦 匹

C#的语法----程序结构(3)

练习2 对于学员成绩的评测  成绩>=90:A  成绩>=80&&成绩<90:B  成绩>=70&&成绩<80:C  成绩>=60&&成绩<70:D  成绩<60:E 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Task

[C++基本语法:从菜鸟变成大佬系列](二):C++的程序结构

C++程序结构 让我们看一下打印Hello World这个词的简单代码. 1 #include <iostream>//头文件名,iostream表示有输入输出流 2 using namespace std; 3 // main() 是主程序开始的地方 4 int main() { 5 cout<<"Hello World"; // 输出Hello World 6 return 0; 7 } 让我们看一下上述程序的各个部分 C ++语言定义了几个标题,其中包含对

【Python】05、python程序结构控制语句

一.程序结构 程序结构:语句和表达式按照什么样的顺序执行 所有语言无非就三种程序结构:        顺序:默认结构 语句从上到下依次一行一行的执行,        分支:选择一个分支执行,永远最多只执行一个分支        循环: 二.分支结构语句 1.Python的比较操作 所有的Python对象都支持比较操作,可用于测试相等性.相对大小等 如果是复合对象,Python会检查其所有部分,包括自动遍历各级嵌套对象,直到可以得出最终结果 测试操作符: " =="操作符测试值的相等性

C语言中的程序结构

C语言中的程序结构有三种,分别是顺序结构.选择结构和循环结构. 一.按照语句的先后顺序执行的程序结构,称为顺序结构. 下面以一个求三角形面积的例子进行说明,其代码如下: 例1.1 1 #include<stdio.h> 2 int main() 3 { 4 int width,height,s; 5 printf("请输入三角形的底宽:\n"); 6 scanf("%d",&width); 7 printf("请输入三角形的高:\n&q

GO语言的进阶之路-go的程序结构以及包简介

GO语言的进阶之路-go的程序结构以及包简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑,编译和运行 A,编辑 Go程序使用UTF-8编码的纯Unicode文本编写.大部分现代编译器都能够自动处理编码,并且某些最流行的编辑器还支持Go语言的语法高亮和自动缩进.如果你用的编辑器不支持Go语言,可以在Go语言官网的搜索框中输入编辑器的名字,看看是否有适合的插件可用.为了编辑方便,所有的Go语言关键字和操作符都使用ASCII编码字符,但是Go语言中标识符可以是任一Uni

C语言学习系列(三)C程序结构

一.C程序结构 C 程序主要包括以下部分: 预处理器指令 函数 变量 语句 & 表达式 注释 new C program demo: 1 #include <stdio.h> /*预处理器指令*/ 2 /* 第一个中文程序实例 */ 3 int main() /*main函数*/ 4 { 5 int i; /*变量*/ 6 i=1; /*语句&表达式*/ 7 printf("我的第%d个C程序\n",i); /*语句&表达式*/ 8 return 0

Verilog HDL的程序结构及其描述

这篇博文是写给要入门Verilog HDL及其初学者的,也算是我对Verilog  HDL学习的一个总结,主要是Verilog HDL的程序结构及其描述,如果有错,欢迎评论指出. 一.Verilog HDL的程序结构 首先我们不开始讲Verilog HDL的语法,我们从Verilog HDL的程序结构出发.相信大家都看过芯片吧,它有个名字,有个外壳,外壳向外伸出有引脚(BGA封装的那种请不要乱搅和...),然后芯片它可以实现一定的功能. Ok,知道这些之后,我们就来看看Verilog HDL的描