.Net基础篇_学习笔记_第六天_for循环的几个练习
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循环的几个练习的更多相关文章
- .Net基础篇_学习笔记_第六天_for循环的嵌套_乘法口诀表
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .Net基础篇_学习笔记_第六天_for循环语法_正序输出和倒序输出
for TAB 和 forr TAB using System; using System.Collections.Generic; using System.Linq; using System. ...
- .Net基础篇_学习笔记_第六天_For循环语法
For循环:专门处理已知循环次数的循环. 小技巧:连续敲击两下TAB键循环体自动搭建完成. For循环语法: for(表达式1;表达式2;表达式3){ 循环体;}表达式1一般为声明循环变量,记录循环 ...
- .Net基础篇_学习笔记_第六天_异常捕获复习及断点调试
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .Net基础篇_学习笔记_第五天_流程控制do-while循环
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .Net基础篇_学习笔记_第六天_类型转换和方法(函数)简介
类型转换:Convert.ToInt32(); int.Parse(); int.TryParse(); Convert.ToInt32(); 的本质就是在使用 int.Parse(); int. ...
- .Net基础篇_学习笔记_第八天_复杂数据类型(常量/枚举/结构)
1.常量: 声明的常量的语法:const 变量类型 变量名=值;最大的特点就是,声明了常量后,就不能再被重新赋值. 2.枚举 语法:[public] enum 枚举名{ 值1, 值2, 值3, ... ...
- .Net基础篇_学习笔记_第七天_随机数的产生
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .Net基础篇_学习笔记_第七天_三元数表达式(if-else的转换写法)
三元表达式语法: 表达式1?表达式2:表达式3; 表达式1一般为一个关系表达式.如果表达式1的值为true,那么表达式2的值就是整个三元表达式的值.如果表达式1的值为false,那么表达式3的值就是整 ...
随机推荐
- ASP.NET Core 框架本质学习
本文作为学习过程中的一个记录. 学习文章地址: https://www.cnblogs.com/artech/p/inside-asp-net-core-framework.html 一. ASP.N ...
- Git原理入门简析
为了获得更好的阅读体验,建议访问原地址:传送门 前言: 之前听过公司大佬分享过 Git 原理之后就想来自己总结一下,最近一忙起来就拖得久了,本来想塞更多的干货,但是不喜欢拖太久,所以先出一版足够入门的 ...
- 入门MySQL——DML语句篇
前言: 在上篇文章中,主要为大家介绍的是DDL语句的用法,可能细心的同学已经发现了.本篇文章将主要聚焦于DML语句,为大家讲解表数据相关操作. 这里说明下DDL与DML语句的分类,可能有的同学还不太 ...
- Mybatis中使用PageHelper插件进行分页
分页的场景比较常见,下面主要介绍一下使用PageHelper插件进行分页操作: 一.概述: PageHelper支持对mybatis进行分页操作,项目在github地址: https://github ...
- python学习之路(3)---列表
列表定义: 列表就是一个数据的集合,列表是可以重复的,可以对存储的数据进行增删改查, 列表的写法: list_name = ['ljwang','wangwu'] 列表的嵌套 a = ['1',['2 ...
- JavaScript最常见的错误种类
1.报错为: Uncaught ReferenceError:未捕获引用错误(引用错误:使用了没有定义的变量) 错误之前的代码会执行,之后代码不会执行 2.报错为: Uncaught Syntaxer ...
- rabbitmq生产者的消息确认
通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以 ...
- python 实现多个线程间消息队列传递,一个简单的列子
#-*-coding:utf8-*-"""Producer and consumer models: 1. There are many producers and co ...
- 移动端 rem单位做适配的 媒体查询节点
@media screen and (min-width:300px){html,body,input{font-size:15px}}@media screen and (min-width:320 ...
- Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)
Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...