我的程序没有错误但它不起作用,它会在退出命令窗口时询问名称和密码。
请帮忙吗?
namespace password
{
class Program
{
static void Main(string[] args)
{
String Samsonpass = "12345";
String Riazpass = "hyperion";
String mypass = "CSGOPRO";
String Samson = "Samson";
String Riaz = "Riaz";
String Curstin = "Curstin";
Console.Write("Enter your name :");
string answer = Console.ReadLine();
Console.Write("Enter your password :");
string password = Console.ReadLine();
if (answer == Curstin && password == mypass)
{
Console.Write("Welcome Curstin");
}
else if (answer == Riaz && password == Riazpass)
{
Console.Write("Welcome Riaz");
}
else if (answer == Samson && password == Samsonpass)
{
Console.Write("Welcome Samson");
}
else
{
Console.Write("Invalid user or password !");
}
}
}
}
答案 0 :(得分:1)
它确实有效,但它完成后会关闭。放一个Console.ReadLine();最后。
答案 1 :(得分:1)
正如其他人所说,只需在程序末尾添加一个console.readline(),如下所示。这将迫使程序停止并给你时间查看输出。
namespace password
{
class Program
{
static void Main(string[] args)
{
String Samsonpass = "12345";
String Riazpass = "hyperion";
String mypass = "CSGOPRO";
String Samson = "Samson";
String Riaz = "Riaz";
String Curstin = "Curstin";
Console.Write("Enter your name :");
string answer = Console.ReadLine();
Console.Write("Enter your password :");
string password = Console.ReadLine();
if (answer == Curstin && password == mypass)
{
Console.Write("Welcome Curstin");
}
else if (answer == Riaz && password == Riazpass)
{
Console.Write("Welcome Riaz");
}
else if (answer == Samson && password == Samsonpass)
{
Console.Write("Welcome Samson");
}
else
{
Console.Write("Invalid user or password !");
}
Console.Readline();
}
}
}
答案 2 :(得分:0)
我猜它确实可以直接以快速结束。 尝试在最后添加ReadLine(并编辑带有姿势的代码;)它看起来很乱)
答案 3 :(得分:0)
完成后暂停代码的两种可能性:
Console.ReadLine();
。或者:答案 4 :(得分:0)
首先,您的错误非常简单,您忘了放Console.ReadKey();
或Console.ReadLine();
,只是在您完成代码后它就不会关闭程序。
其次我建议您使用 开关 ,我不太确定变量名称是否与此应用非常相关,它不是用户名,是吗?。你必须做这样的事情:
static void Main(string[] args)
{
Console.Write("Enter your name: ");
string answer = Console.ReadLine();
Console.Write("Enter your password: ");
string password = Console.ReadLine();
switch (password)
{
case "12345":
Console.WriteLine("Welcome " + answer);
break;
case "hyperion":
Console.WriteLine("Welcome" + answer);
break;
case "CSGOPRO":
Console.WriteLine("Welcome " + answer);
break;
default:
break;
}
Console.ReadKey();
}
切换参考:https://msdn.microsoft.com/en-us/library/06tc147t.aspx 祝你好运!