.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的值就是整 ...
随机推荐
- exe4j打包--exe转安装包
前面一篇已经详细的说明了打包成exe的步骤了,下面谈谈exe如何压缩成安装文件.这里用到之前的另外一个软件,具体软件看这篇文章 exe4j打包成exe 打开inno 编辑器 打开软件后我们选择 用[脚 ...
- 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)
Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...
- pycharm的补充
pycharm 快捷键 tab自动补全 首行缩进 ctrl+?是全行加#进行注释 ctrl+d 复制上一行 ctrl +z 撤销 ctrl+shift+z 撤销的撤销 更改字体大小
- 贪心算法-过河问题 pojo1700
过桥问题: 黑夜,只有一只手电筒 A过桥需要1s B过桥需要3s C过桥需要5s D过桥需要8s E过桥需要12s 求最小过桥时间 贪心算法: 从最大的开始过去,最小的两个做为辅助. 假如左岸人数为2 ...
- pyinstaller打包django项目成exe以及遇到的一些问题
1. 环境 环境为:win7 + Python3.6 + Django (2.1.10) + PyInstaller (3.5) 注意: 这里的PyInstaller,笔者是直接使用pip安装,中间会 ...
- HDU 6299
题意略. 思路: 我们先把所有字符串处理成 ")))((((" 这样的形式.然后我们把这些字符串排个序,按照min(l,r)来排,小的在前,在我的代码中,l为 ( 的个数,r 为 ...
- PHP CURL根据详细地址获取腾讯地图经纬度
<?php $address = "广东省广州市天河区"; $point = getPoint($address); var_dump($point);//输出经纬度 /** ...
- Delphi - Indy TIdThreadComponent 线程研究
Indy IdThreadComponent 线程研究 前几天在开发数据实时解析功能模块的时候,发现解析数据量巨大,特别耗时,程序一跑起来界面假死. 为了优化用户体验,采用了Indy 自带的IdThr ...
- Python 基础(五)
pandas缺失值处理 import pandas as pd importrandom df01 = pd.DataFrame(np.random.randint(1,9),size = (4,4) ...
- odoo12从零开始:二、个性化定制odoo12 之 创建数据库页面
剧情回顾 上一文章,我们已经成功运行了odoo12,并访问localhost:8069看到如下界面: 我们还没有创建数据库,但是我们发现,数据库管理页面的logo是odoo,数据库页面全是英文的,对于 ...