C# 控制台密码显示星号
怎么实现输入的密码是星号?usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem...
怎么实现输入的密码是星号?using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConX{ class Program { static void Main(string[] args) { Console.WriteLine("欢迎使用本程序\n"); Console.Write("请输入用户名:"); var user = Console.ReadLine(); Console.Write("请输入密码:"); var pw = Console.ReadLine(); while (user != "admin" || pw != "nmnba456") { Console.Clear(); Console.WriteLine("用户名或密码错误"); Console.Write("请重新输入用户名:"); user = Console.ReadLine(); Console.Write("请重新输入密码:"); pw = Console.ReadLine(); } Console.WriteLine("用户名及密码正确"); Console.ReadKey(); } }}
展开
1个回答
展开全部
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConX { class Program { static void Main(string[] args) { Console.WriteLine("欢迎使用本程序\n"); Console.Write("请输入用户名:"); var user = Console.ReadLine(); Console.Write("请输入密码:"); var pw = ReadPasswd(); while (user != "admin" || pw != "nmnba456") { Console.Clear(); Console.WriteLine("用户名或密码错误"); Console.Write("请重新输入用户名:"); user = Console.ReadLine(); Console.Write("请重新输入密码:"); pw = ReadPasswd(); } Console.WriteLine("用户名及密码正确"); Console.ReadKey(); } private static string ReadPasswd() { StringBuilder sb = new StringBuilder(); ConsoleKeyInfo k; while (true) { k = Console.ReadKey(); if (k.Key == ConsoleKey.Enter) { return sb.ToString(); } Console.CursorLeft--; Console.Write("*"); sb.Append(k.KeyChar); } } } } 这样就行了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询