如何实现用于在键入用户密码时隐藏用户密码的代码。 [英] How do I implement my code used to hide the users password while they are typing it.
本文介绍了如何实现用于在键入用户密码时隐藏用户密码的代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间ConsoleApplication5
{
类程序
{
公共静态字符串ReadPassword()
{
string password = ;
ConsoleKeyInfo info = Console.ReadKey(true);
while(info.Key!= ConsoleKey.Enter)
{
if(info.Key!= ConsoleKey.Backspace)
{
Console.Write(* );
密码+ = info.KeyChar;
}
else if(info.Key == ConsoleKey.Backspace)
{
if(!string.IsNullOrEmpty(password))
{
/ /从密码字符列表中删除一个字符
password = password.Substring(0,password.Length - 1);
//获取游标的位置
int pos = Console.CursorLeft;
//将光标向左移动一个字符
Console.SetCursorPosition(pos - 1,Console.CursorTop);
//用空格替换
Console.Write();
//再次将光标向左移动一个字符
Console.SetCursorPosition(pos - 1,Console.CursorTop);
}
}
info = Console.ReadKey(true);
}
//添加一个新行,因为用户在密码结束时按Enter键
Console.WriteLine();
返回密码;
}
static void Main(string [] args)
{
Random numberMaker = new Random();
int number = numberMaker.Next(1,100);
Console.WriteLine(请在下面输入你的名字。);
Console.Write(Name:);
string name = Console.ReadLine();
if(name ==)
{
Console.WriteLine(\ nERROR);
name =ERROR;
}
Console.WriteLine(\我想的是1到100之间的数字。);
int guess = 0;
do
{
Console.Write(猜猜:);
guess = int.Parse(Console.ReadLine());
if(number> guess)
{
Console.WriteLine(\ nToo low);
}
else if(number< guess)
{
Console.WriteLine(\ nToo high);
}
else if(name ==Logan)
{
string PASSWORD =998783;
Console.Write(\ nPassword:);
string password = Console.ReadLine();
if(password == PASSWORD)
{
Console.WriteLine(\\\
Access Granted。);
Console.WriteLine(\\\
Welcome {0} .\ n,name);
int num1;
int num2;
字符串操作数;
浮动答案;
Console.Write(\\\
Please输入第一个整数:);
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write(请输入一个操作数(+, - ,/,*):);
operand = Console.ReadLine();
Console.Write(请输入第二个整数:\ n);
num2 = Convert.ToInt32(Console.ReadLine());
开关(操作数)
{
case - :
answer = num1 - NUM2;
休息;
case+:
answer = num1 + num2;
休息;
case/:
answer = num1 / num2;
休息;
case*:
answer = num1 * num2;
休息;
默认值:
answer = 0;
休息;
}
Console.WriteLine(num1.ToString()++操作数++ num2.ToString()+ =+ answer.ToString());
}
else
{
Console.WriteLine(\ nERROR);
休息;
}
}
其他
{
Console.WriteLine(\ nYou win!);
}
} while(number!= guess);
Console.ReadLine();
Console.WriteLine(再见,{0}。,名称);
Console.ReadLine();
}
}
}
解决方案
什么......?
您从此处获得了ReadPassword方法: http://rajeshbailwal.blogspot .hu / 2012/03 / password-in-c-console-application.html [ ^ ]。你根本就不使用它。而不是
string password = Console.ReadLine();
使用
string password = ReadPassword();
但pelase尝试至少做你的功课....
问Google,例如密码屏蔽控制台应用程序 [ ^ ]。
干杯
Andi
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication5 { class Program { public static string ReadPassword() { string password = ""; ConsoleKeyInfo info = Console.ReadKey(true); while (info.Key != ConsoleKey.Enter) { if (info.Key != ConsoleKey.Backspace) { Console.Write("*"); password += info.KeyChar; } else if (info.Key == ConsoleKey.Backspace) { if (!string.IsNullOrEmpty(password)) { // remove one character from the list of password characters password = password.Substring(0, password.Length - 1); // get the location of the cursor int pos = Console.CursorLeft; // move the cursor to the left by one character Console.SetCursorPosition(pos - 1, Console.CursorTop); // replace it with space Console.Write(" "); // move the cursor to the left by one character again Console.SetCursorPosition(pos - 1, Console.CursorTop); } } info = Console.ReadKey(true); } // add a new line because user pressed enter at the end of their password Console.WriteLine(); return password; } static void Main(string[] args) { Random numberMaker = new Random(); int number = numberMaker.Next(1, 100); Console.WriteLine("Please input your name below."); Console.Write("Name: "); string name = Console.ReadLine(); if (name == "") { Console.WriteLine("\nERROR"); name = "ERROR"; } Console.WriteLine("\nI'm thinking of a number between 1 and 100."); int guess = 0; do { Console.Write("Take a guess: "); guess = int.Parse(Console.ReadLine()); if (number > guess) { Console.WriteLine("\nToo low"); } else if (number < guess) { Console.WriteLine("\nToo high"); } else if (name == "Logan") { string PASSWORD = "998783"; Console.Write("\nPassword: "); string password = Console.ReadLine(); if (password == PASSWORD) { Console.WriteLine("\nAccess Granted."); Console.WriteLine("\nWelcome {0}.\n", name); int num1; int num2; string operand; float answer; Console.Write("\nPlease enter the first integer: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Please enter an operand (+, -, /, *): "); operand = Console.ReadLine(); Console.Write("Please enter the second integer: \n"); num2 = Convert.ToInt32(Console.ReadLine()); switch (operand) { case "-": answer = num1 - num2; break; case "+": answer = num1 + num2; break; case "/": answer = num1 / num2; break; case "*": answer = num1 * num2; break; default: answer = 0; break; } Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString()); } else { Console.WriteLine("\nERROR"); break; } } else { Console.WriteLine("\nYou win!"); } } while (number != guess); Console.ReadLine(); Console.WriteLine("Good bye, {0}.",name); Console.ReadLine(); } } }
解决方案
What the... ?
You got the ReadPassword method from here: http://rajeshbailwal.blogspot.hu/2012/03/password-in-c-console-application.html[^]. You simply don't use it. Instead of
string password = Console.ReadLine();
use
string password = ReadPassword();
But pelase try at least to do your homework....
Ask Google, e.g. Password masking console application[^].
Cheers
Andi
这篇关于如何实现用于在键入用户密码时隐藏用户密码的代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文