zoukankan      html  css  js  c++  java
  • C#简单的四位纯数字验证码

    验证码练手,整型、四位验证码

    大体意思就是:四位纯数字验证,只要验证不成功就无限验证

    刚开始在纠结怎么让整个过程循环起来,什么循环放到最外层,其实就是一个循环,看来自己的循环练习的还是不够多,不够灵活

    看代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _11._1练习
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             while (true)
    14             {
    15 
    16 
    17                 //随机验证码     整型
    18                 Random x = new Random();
    19                 int ranx = x.Next(1000, 9999);
    20 
    21 
    22 
    23 
    24                 //把整型的随机数转换成字符串,方便进行字符串比对
    25                 string ran = Convert.ToString(ranx);
    26 
    27 
    28 
    29                 //打印验证码     
    30                 Console.WriteLine("验证码:" + ranx);
    31                 Console.WriteLine();
    32                 Console.Write("验  证:");
    33 
    34 
    35 
    36 
    37                 //获取用户输入的内容
    38                 string user = Console.ReadLine();
    39 
    40 
    41 
    42                 //防止用户误操作,把空格替换成空字符串
    43                 string str = user.Replace(" ", "");
    44 
    45 
    46 
    47 
    48                 //检测用户输入内容的长度,长度符合---下一步,长度不符合---长度错误
    49                 int strleg = str.Length;
    50                 if (strleg == 4)
    51                 {
    52                     //开始比对字符串
    53 
    54                     if (str == ran)
    55                     {
    56                         Console.WriteLine();
    57                         Console.ForegroundColor = ConsoleColor.Red;
    58                         Console.WriteLine("验证成功!!!");
    59                         break;
    60                     }
    61                     else
    62                     {
    63                         Console.ForegroundColor = ConsoleColor.Red;
    64                         Console.WriteLine();
    65                         Console.WriteLine("输入错误!!!");
    66                         Console.ForegroundColor = ConsoleColor.White;
    67                         Console.WriteLine();
    68                     }
    69                 }
    70                 else
    71                 {
    72                     Console.ForegroundColor = ConsoleColor.Red;
    73                     Console.WriteLine();
    74                     Console.WriteLine("长度错误!!!");
    75                     Console.ForegroundColor = ConsoleColor.White;
    76                     Console.WriteLine();
    77                 }
    78             }
    79 
    80 
    81 
    82 
    83             Console.ReadLine();
    84         }
    85     }
    86 }

    为了方便检查代码,加了挺多的注释,并且每个过程都会空格开,个人感觉这个习惯挺好,因为在检查代码或者是卡断点的时候,自己的思路更清晰、更透彻

    嗯,放一张效果图吧,小黑窗其实也蛮有意思的

    还是需要多练习,键盘上贴纸了,看什么时候能把键盘纸敲烂

  • 相关阅读:
    python 基础2.5 循环中continue与breake用法
    python 基础 2.4 while 循环
    python 基础 2.3 for 循环
    python 基础 2.2 if流程控制(二)
    python 基础 2.1 if 流程控制(一)
    python 基础 1.6 python 帮助信息及数据类型间相互转换
    python 基础 1.5 python数据类型(四)--字典常用方法示例
    Tornado Web 框架
    LinkCode 第k个排列
    LeetCode 46. Permutations
  • 原文地址:https://www.cnblogs.com/TheJoker/p/7766588.html
Copyright © 2011-2022 走看看