zoukankan      html  css  js  c++  java
  • .Net基础篇_学习笔记_第六天_for循环的几个练习

     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             bool b = true;                                                                             //让代码满足某些条件的时候才去执行
    14             int sum = 0;
    15             for (int i = 1; i <=5; i++)
    16             {
    17                 Console.WriteLine("请输入录入第{0}个人的年龄为:",i);
    18                 try
    19                 {
    20                     int _age = Convert.ToInt32(Console.ReadLine());
    21                     if (_age > 0 && _age < 100)
    22                     {
    23                         sum += _age;
    24                     }
    25                     else
    26                     {
    27                         b = false;
    28                         Console.WriteLine("您输入的年龄不在范围内,程序退出");
    29                         break;
    30                     }
    31                 }
    32                 catch
    33                 {
    34                     Console.WriteLine("您输入的年龄格式不正确,程序退出");
    35                     b = false;
    36                     break;
    37                 }
    38 
    39             }
    40             if (b)
    41             {
    42                 Console.WriteLine("您录入5个人的平均年龄为:{0}岁", sum / 5);
    43             }
    44            
    45             Console.ReadKey();
    46         }
    47     }
    48 }
    bool类型的应用,让代码满足某些条件的时候才去执行。

    练习二:用户名和密码的登陆——while循环书写
     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             Console.WriteLine("请输入您的用户名:");
    14             string _name=Console.ReadLine();
    15             Console.WriteLine("请输入您的密码");
    16             string _psd= Console.ReadLine();
    17             while (_name!="admin"||_psd!="8888")
    18             {
    19                 Console.WriteLine("
    请输入的用户名或密码有误,请重新输入");
    20                 Console.WriteLine("请再次输入您的用户名:");
    21                 _name = Console.ReadLine();
    22                 Console.WriteLine("请再次输入您的密码");
    23                 _psd = Console.ReadLine();
    24                
    25             }
    26             Console.WriteLine("您输入的用户名和密码正确,程序可以被使用");
    27             Console.ReadKey();
    28         }
    29     }
    30 }

    另一种方式

     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             while (true)
    14             {
    15                 Console.WriteLine("请输入您的用户名:");
    16                 string _name = Console.ReadLine();
    17                 Console.WriteLine("请输入您的密码");
    18                 string _psd = Console.ReadLine();
    19                 if (_name == "admin" || _psd == "8888")
    20                 {
    21                     Console.WriteLine("您输入的用户名和密码正确,程序可以被使用");
    22                     break;
    23                 }
    24                 else
    25                 {
    26                    
    27                     Console.WriteLine("
    请输入的用户名或密码有误,请重新输入");
    28                 }
    29                 
    30             }   
    31             Console.ReadKey();
    32         }
    33     }
    34 }

     练习三:

     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             for (int i = 1; i <=100; i++)
    15             {
    16                 sum += i;
    17                 if (sum>20)
    18                 {
    19                     Console.WriteLine("累加值大于20的当前数是{0}",i);
    20                     break;
    21                 }
    22             }
    23                
    24             Console.ReadKey();
    25         }
    26     }
    27 }
  • 相关阅读:
    ASP程序调用验证码
    fcex pf.conf
    高速INTERNET代理服务器解决方案
    《IIS 6的几个经典问答》
    一个符合WEB标准的横向下拉菜单
    FREEBSD+PF 在6.2上的架设
    权限
    FreeBSD+IPFILTER实现整网(N个Vlan)透明代理上网
    pf 带宽控制 例子
    FreeBSD下配置DHCP服务小结
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/7216012.html
Copyright © 2011-2022 走看看