C# 我的程序中有逻辑错误吗?
我的程序没有错误,但它不工作,它要求一个名称和密码,然后退出命令窗口 需要帮忙吗C# 我的程序中有逻辑错误吗?,c#,if-statement,passwords,C#,If Statement,Passwords,我的程序没有错误,但它不工作,它要求一个名称和密码,然后退出命令窗口 需要帮忙吗 namespace password { class Program { static void Main(string[] args) { String Samsonpass = "12345"; String Riazpass = "hyperion"; String mypass = "CS
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 !");
}
}
}
}
我想它能正常工作,只是结束得很快。
尝试在末尾添加ReadLine(并编辑错误代码;)它看起来很凌乱)我想它可以正常工作,只是结束得很快。
尝试在末尾添加ReadLine(并编辑错误代码;)它看起来很凌乱)它确实可以工作,但完成后就关闭了。放置一个控制台。ReadLine();最后。它确实起作用,但完成后就关闭了。放置一个控制台。ReadLine();正如其他人所说,只需在程序末尾添加一个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();
}
}
}
正如其他人所说,只需在程序末尾添加一个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();
}
}
}
完成后暂停代码的两种可能性:
Console.ReadLine()代码>在代码末尾。或:
完成后暂停代码的两种可能性:
Console.ReadLine()代码>在代码末尾。或:
首先,您的错误很简单,您忘记了put
Console.ReadKey()代码>或控制台.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();
}
开关参考:
祝你好运 首先,你的错误很简单,你忘了放Console.ReadKey()代码>或控制台.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();
}
开关参考:
祝你好运 因为它已无事可做,所以它将退出。如果需要保持打开,请考虑沿着代码>控制台。程式结束时的程式码>。直接在命令介面中呼叫程式exe。然后,命令窗口将不会关闭,您可以看到输出,因为它已无事可做,因此将退出。如果需要保持打开,请考虑沿着代码>控制台。程式结束时的程式码>。直接在命令介面中呼叫程式exe。然后命令窗口将不会关闭,你可以看到输出谢谢你这么多!你帮了大忙:)非常感谢!你帮了大忙:)