using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 第六天_流程语句
{
class Program
{
static void Main(string[] args)
{
bool b = true; //让代码满足某些条件的时候才去执行
int sum = ;
for (int i = ; i <=; i++)
{
Console.WriteLine("请输入录入第{0}个人的年龄为:",i);
try
{
int _age = Convert.ToInt32(Console.ReadLine());
if (_age > && _age < )
{
sum += _age;
}
else
{
b = false;
Console.WriteLine("您输入的年龄不在范围内,程序退出");
break;
}
}
catch
{
Console.WriteLine("您输入的年龄格式不正确,程序退出");
b = false;
break;
} }
if (b)
{
Console.WriteLine("您录入5个人的平均年龄为:{0}岁", sum / );
} Console.ReadKey();
}
}
}
bool类型的应用,让代码满足某些条件的时候才去执行。

练习二:用户名和密码的登陆——while循环书写
 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 _name=Console.ReadLine();
Console.WriteLine("请输入您的密码");
string _psd= Console.ReadLine();
while (_name!="admin"||_psd!="")
{
Console.WriteLine("\n请输入的用户名或密码有误,请重新输入");
Console.WriteLine("请再次输入您的用户名:");
_name = Console.ReadLine();
Console.WriteLine("请再次输入您的密码");
_psd = Console.ReadLine(); }
Console.WriteLine("您输入的用户名和密码正确,程序可以被使用");
Console.ReadKey();
}
}
}

另一种方式

 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("请输入您的用户名:");
string _name = Console.ReadLine();
Console.WriteLine("请输入您的密码");
string _psd = Console.ReadLine();
if (_name == "admin" || _psd == "")
{
Console.WriteLine("您输入的用户名和密码正确,程序可以被使用");
break;
}
else
{ Console.WriteLine("\n请输入的用户名或密码有误,请重新输入");
} }
Console.ReadKey();
}
}
}

练习三:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 第六天_流程语句
{
class Program
{
static void Main(string[] args)
{
int sum = ;
for (int i = ; i <=; i++)
{
sum += i;
if (sum>)
{
Console.WriteLine("累加值大于20的当前数是{0}",i);
break;
}
} Console.ReadKey();
}
}
}

.Net基础篇_学习笔记_第六天_for循环的几个练习的更多相关文章

  1. .Net基础篇_学习笔记_第六天_for循环的嵌套_乘法口诀表

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. .Net基础篇_学习笔记_第六天_for循环语法_正序输出和倒序输出

    for TAB  和 forr TAB using System; using System.Collections.Generic; using System.Linq; using System. ...

  3. .Net基础篇_学习笔记_第六天_For循环语法

    For循环:专门处理已知循环次数的循环.  小技巧:连续敲击两下TAB键循环体自动搭建完成. For循环语法: for(表达式1;表达式2;表达式3){ 循环体;}表达式1一般为声明循环变量,记录循环 ...

  4. .Net基础篇_学习笔记_第六天_异常捕获复习及断点调试

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. .Net基础篇_学习笔记_第五天_流程控制do-while循环

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. .Net基础篇_学习笔记_第六天_类型转换和方法(函数)简介

    类型转换:Convert.ToInt32();  int.Parse();  int.TryParse(); Convert.ToInt32(); 的本质就是在使用 int.Parse(); int. ...

  7. .Net基础篇_学习笔记_第八天_复杂数据类型(常量/枚举/结构)

    1.常量: 声明的常量的语法:const 变量类型 变量名=值;最大的特点就是,声明了常量后,就不能再被重新赋值. 2.枚举 语法:[public] enum 枚举名{ 值1, 值2, 值3, ... ...

  8. .Net基础篇_学习笔记_第七天_随机数的产生

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. .Net基础篇_学习笔记_第七天_三元数表达式(if-else的转换写法)

    三元表达式语法: 表达式1?表达式2:表达式3; 表达式1一般为一个关系表达式.如果表达式1的值为true,那么表达式2的值就是整个三元表达式的值.如果表达式1的值为false,那么表达式3的值就是整 ...

随机推荐

  1. 《机器学习基石》---VC维

    1 VC维的定义 VC维其实就是第一个break point的之前的样本容量.标准定义是:对一个假设空间,如果存在N个样本能够被假设空间中的h按所有可能的2的N次方种形式分开,则称该假设空间能够把N个 ...

  2. Spring Boot之Profile--快速搞定多环境使用与切换

    Spring Profile是Spring3引入的概念,主要用在项目多环境运行的情况下,通过激活方式实现多环境切换,省去多环境切换时配置参数和文件的修改,并且Spring profile提供了多种激活 ...

  3. 自定义GroupBox

    public partial class mGroupBox : GroupBox { private Color _TitleBackColor = Color.Black; private Fon ...

  4. T-SQL基础语句

    存储过程允许标准组件式编程(模块化设计) 存储过程能够实现快速的执行速度 存储过程能够减少网络流量 存储过程可被作为一种安全机制充分利用 在SQL Server 的系列版本中存储过程分为两类:系统提供 ...

  5. Groovy语法基础

    Groovy 简介 Groovy 是一种基于 JVM 的动态语言,他的语法和 Java 相似,最终也是要编译 .class 在JVM上运行. Groovy 完全兼容 Java 并且在此基础上添加了很多 ...

  6. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

  7. 玩转SpringBoot 2 快速搭建 | Spring Initializr 篇

    SpringBoot 为我们提供了外网 Spring Initializr 网页版来帮助我们快速搭建 SpringBoot 项目,如果你不想用 IDEA 中的插件,这种方式也是不错的选择.闲话少说,直 ...

  8. python 09 函数

    目录 函数初识 1. 函数定义: 2. 函数调用: 3. 函数的返回值: 4. () 4.1 位置传参: 4.2 关键字传参: 4.3 混合传参: 函数初识 1. 函数定义: def 函数名(): 函 ...

  9. CentOS -- Zookeeper installation and configure

    1 JDK 1.8 must installed first 2 Get Zookeeper package wget https://archive.apache.org/dist/zookeepe ...

  10. CodeForces 989C

    题意略. 思路:如图 详见代码: #include<bits/stdc++.h> #define maxn 55 using namespace std; char board[maxn] ...