对C#中几个循环语句的使用,请教
今天是在云和数据学院学习的第四天,由于各种原因···今天自己预习的循环语句的用法以及写了几个程序,也遇到各种的问题了···纠结。由于还是在学习的很初初初级,所以好多简单的方法还是不知道怎么写出来,只得硬拉几个循环语句。下面就看下我今天写的十几个小程序。
<1>.输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩
static void Main(string[] args)
{
int i = ; int sum = ; int avg = ;
Console.WriteLine("请输入班级总人数:");
int man = Convert.ToInt32(Console.ReadLine());
for (; i <= man;i++)
{
Console.WriteLine("请各输入学生的成绩:");
int score = Convert.ToInt32(Console.ReadLine());
sum = sum + score;
avg = sum / man;
}
Console.WriteLine("平均成绩为{0},总成绩为{1}", avg,sum);
Console.ReadLine();
}
<2>打印100次“欢迎您来云和学院学习"
static void Main(string[] args)
{
int i=;
while (i < )
{
Console.WriteLine("欢迎您来云和学院学习");
}
Console.ReadLine();
}
<3>2006年培养学员80000人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?
static void Main(string[] args)
{
double sdt = ;
int year = ;
sdt = 1.25 * sdt;
do
{
year++;
}
while(sdt > );
{
Console.WriteLine("到{0}培训学员人数将达到20万人",year);
Console.ReadKey();
}
}
<4>/老师问学生,这道题你会做了吗?如果学生答"会了(y)",则可以放学.如果学生不会做(n),则老师再讲一遍,再问学生是否会做了......
-直到学生会为止,才可以放学.
-直到学生会或老师给他讲了10遍还不会,都要放学
static void Main(string[] args)
{
Console.WriteLine("这道题你会做了吗?");
Console.WriteLine("y/n");
string anw = Console.ReadLine();
if (anw =="y")
{
Console.WriteLine("可以放学");
}
else
{
Console.WriteLine("老实继续讲,这道题你会做了吗?");
Console.WriteLine("y/n");
string anw1 = Console.ReadLine();
if (anw1 == "y")
{
Console.WriteLine("放学");
}
else
{
int i;
for (i=; i <= ; i++)
{
Console.WriteLine("老师再讲一遍");
Console.WriteLine("y/n");
}
Console.WriteLine("都要放学");
}
}
Console.ReadKey();
}
这个 整的我好迷茫啊,甚是不知道该怎么解决???真的是太无奈啊,学的只是还不多···想不出来该如何解决····请教。
<5>请用户输年份,再输入月份,输出该月的天数.(结合之前如何判断闰年来做)
static void Main(string[] args)
{
Console.WriteLine("请用户输年份:");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请用户输月份:");
int month = Convert.ToInt32(Console.ReadLine());
switch (month)
{
case :
case :
case :
case :
case :
case :
case :Console.WriteLine("您输入的月份共有31天");
break;
case :
bool a =year % == || year % == && year % != ;
switch (a)
{
case true : Console.WriteLine("您输入的月份共有29天");
break;
case false: Console.WriteLine("您输入的月份共有28天");
break;
}
break;
case :
case :
case :
case :Console.WriteLine("您输入的月份共有31天");
break;
}
Console.ReadLine();
}
这个写的还算满意吧,但是我还是想有更好的方法吧,是不是下面分四种情况就输出来啦那???
<6> 这个是尝试下用while的用法:打印100次“欢迎您来云和学院学习"
static void Main(string[] args)
{
int i=;
while (i < )
{
Console.WriteLine("欢迎您来云和学院学习");
}
Console.ReadLine();
}
<7>这个是练习下switch···case···的用法:对学员的结业考试成绩评测(改成用Switch来做) 成绩>=90:A 90>成绩>=80:B 80>成绩>=70:C 70>成绩>=60:D 成绩<60:E
static void Main(string[] args)
{
Console.WriteLine("请输入你的成绩");
string Score = Console.ReadLine();
int score = Convert.ToInt32(Score);
switch (score/)
{
case :
case :
case :
case :
case :
case :
case : Console.WriteLine("E");
break;
case : Console.WriteLine("E");
break;
case : Console.WriteLine("E");
break;
case : Console.WriteLine("E");
break;
}
Console.ReadLine();
}
感觉switch用着好痛苦啊!!! 还是if···else···好用,简单易懂,哇咔咔.
<8>这个也是while的用法,感觉好乱啊,嘿嘿 程序要求:要求用户输入用户名和密码,只要不是admin、888888就一直提示用户名或密码错误,请重新输入。
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入用户名:");
string a = Console.ReadLine();
Console.WriteLine("请输入密码:");
string b = Console.ReadLine();
if (a != "admin" || b != "")
{
Console.WriteLine("用户名或密码错误,请重新输入.");
}
else
{
Console.WriteLine("用户名或密码正确,请进入.");
}
Console.ReadKey();
}
}
<9>for循环来啦,激动啊,嘿嘿 程序要求:计算1到100之间整数的和
static void Main(string[] args)
{
int i = ;
int sum = ;
for (; i <= ; i++)
{
sum =sum+ i;
}
Console.WriteLine("输出1到100之间整数的和"+sum);
Console.ReadLine();
}
<10>明天小兰就要登台演出了,老师说再把明天的演出的歌曲唱一遍,
如果满意,小兰就可以回家了.
否则就需要再练习一遍,直到老师满意为止.(y/n)
static void Main(string[] args)
{
Console.WriteLine("小兰唱歌满意不满意,如果满意可以回家,不满意继续练习");
string a = Console.ReadLine();
int i = ;
do
{ if (a == "n")
{
Console.WriteLine("继续练习");
a = Console.ReadLine();
i++;
}
else
{
Console.WriteLine("可以回家"); } }
while (a == "n"&&i<=);
{
} {
Console.WriteLine("小兰唱歌满意");
Console.WriteLine("满意可以回家");
Console.ReadKey(); }
}
这个用的do···while···太纠结啦啦啦,不知道是在do里面写循环体还是写这样的条件,我想说我就这样运行除恶要的结果啦。
<11>不断要求用户输入学生姓名,输入q结束
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入学生姓名:");
string input = Console.ReadLine();
if (input == "q")
{
break;
}
else
{
Console.WriteLine("请再次输入学生姓名:");
}
Console.ReadKey();
}
}
这个输出的结果好逗,但是要求的还是循环出来啦,只是感觉程序这样写还是感觉不对啊
<12>下面这个也是用的switch···case···,感觉不怎么好玩,还是if···else···用起来简单方便啊,程序要求:李四的年终工作评定,如果定为A级,则工资涨500元,如果定为B级,则工资涨200元,如果定为C级,工资不变,如果定为D级工资降200元,如果定为E级工资降500元.设李四的原工资为5000,请用户输入李四的评级,然后显示李四来年的工资.
static void Main(string[] args)
{
Console.WriteLine("请输入对李四的评定等级(A-E)?");
string str = Console.ReadLine();
decimal salary = ;
bool flag = false;
switch (str)
{
case "A":
salary += ;
break;
case "B":
salary += ;
break;
default:
Console.WriteLine("你的输入有问题!");
flag = true;
break;
case "C":
break;
case "D":
salary -= ;
break;
case "E":
salary -= ;
break;
}
if (flag == false)
{
Console.WriteLine("李四的工资为:" + salary);
}
Console.ReadKey();
}
<f13>for循环来啦:求1-100间的所有偶数和
static void Main(string[] args)
{
int i = ; int sum = ;
for (; i <= ; i=i+)
{
sum = sum + i;
}
Console.WriteLine("1-100间的所有偶数和为" + sum);
Console.ReadKey();
}
好啦,先写这几个吧,我学习.net的历程····
明天可以学习下这2个题目啦吧
•问题3:输出九九乘法表(循环的嵌套,嘿嘿
对C#中几个循环语句的使用,请教的更多相关文章
- 20.SqlServer中if跟循环语句
--if语句declare @i int begin print @i end else --循环语句 declare @i int begin insert into grade(classname ...
- sql server中的while循环语句
语法格式: while 条件 begin ....... end declare @num begin update SDetail end
- python基础学习(五)while循环语句
while循环基本使用 循环的作用就是让指定的代码重复的执行 while循环最常用的应用场景就是让执行的代码按照指定的次数重复执行 流程图 基本语法 初始条件设置 —— 通常是重复执行的 计数器 wh ...
- MATLAB 的循环语句
1.MATLAB while循环语法 在MATLAB 中 while循环的语法如下: while <expression> <statements> end while 循环反 ...
- awk循环语句-【AWK学习之旅】
AWK中两种循环语句:if-else 和 while 控制流语句: 1.if-else 求总数,平均值: [root@monitor awkdir]# awk '$3>6 {n = n ...
- [Objective-C语言教程]循环语句(9)
当需要多次执行同一代码块时,可以使用循环来解决. 通常,语句按顺序执行:首先执行函数中的第一个语句,然后执行第二个语句,依此类推. 编程语言提供各种控制结构,允许更复杂的执行路径.循环语句可用于多次执 ...
- Golang教程:循环语句
循环语句用于重复执行一段代码. for 语句是 Go 中唯一的循环语句.Go 没有提供其他语言(如 C)中的 while 和 do while 语句. for 语句语法 for 语句的语法如下: fo ...
- shell脚本学习—条件测试和循环语句
条件测试 1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假, 则命令的Exit Status为1(注意与 ...
- Java编程基础-选择和循环语句
一.选择结构语句 选择结构:也被称为分支结构.选择结构有特定的语法规则,代码要执行具体的逻辑运算进行判断,逻辑运算的结果有两个,所以产生选择,按照不同的选择执行不同的代码. Java语言提供了两种选择 ...
随机推荐
- (笔记)Mysql命令desc:获取数据表结构
desc命令用于获取数据表结构. desc命令格式: desc 表名;同样 show columns from 表名;也能获取数据表结构. 举例如下:mysql> desc MyCl ...
- e611. Setting Focus Traversal Keys for the Entire Application
This example changes the focus traversal keys for the entire application. For an example of how to c ...
- Eclipse中创建Maven项目失败
Eclipse中创建Maven项目报错:Unable to create project from archetype org.apache.maven.archetypes:maven-archet ...
- 【设计和开发一套简单自己主动化UI框架】
!有兴趣的朋友请直接移步Github,本帖子已经不做更新,框架的详细的实现已经做了优化和代码整理,本文仅仅介绍了详细的设计思路! 目标:编写一个简单通用UI框架用于管理页面和完毕导航跳转 终于的实现效 ...
- ibatis中的安全问题
http://blog.csdn.net/yangqillohe/article/details/4139265
- [SQL Server] 复制数据库任务
假设你要生产环境下的数据做相应的测试,比如修改及测试存储过程.更改和优化索引等.但是你用户在连接数据库的情况下,你又不能断开数据库的连接.如何取得数据库的副本呢? 一. 利用数据库任务中的复制数据库 ...
- 使用DUPLICATE 方式创建ORACLE 11G DG备库环境
我的最佳实践 ① 手动创建好初始化参数文件: *.audit_file_dest='E:\APP\XJXU\ADMIN\ORASTAND\ADUMP'*.control_files='E:\APP\X ...
- keep-alive pipeline区别
在http1.1中,引入了一种新的特性,即pipeline.那么什么是pipeline呢?pipeline其实就是流水线作业,它可以看作为keepalive的一种升华,因为pipeline也是基于长连 ...
- 为npm设置代理
npm全称为Node Packaged Modules.它是一个用于管理基于node.js编写的package的命令行工具.其本身就是基于node.js写的,这有点像gem与ruby的关系. 在我们的 ...
- liunx 时间ntp同步服务器
1.root 用户下安装 yum install ntp -y 报错如下: 29 Apr 00:25:04 ntpdate[8786]: the NTP socket is in use, exiti ...