if和else if语句C#

时间:2018-10-05 22:56:24

标签: c#

嗨,当我输入“是”或“否” /“否”时,它什么都不做,应该做错了吗?

enter image description here

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Username:");
        String user = Console.ReadLine();
        Console.WriteLine("Password:");
        String Pass = Console.ReadLine();

        Console.WriteLine("Are You Sure Your User: " + user + " And Pass: " + Pass + " is correct?  Yes/No");
        Console.ReadLine();
        if (Console.ReadLine() == "Yes" || Console.ReadLine() == "yes")
        {
            Console.WriteLine("You May now close this");
        }
        else if(Console.ReadLine() == "No" || Console.ReadLine() == "no")
        {
            Console.WriteLine("Pleas Press enter");
            System.Diagnostics.Process.Start(Environment.GetCommandLineArgs()[0], Environment.GetCommandLineArgs().Length > 1 ? string.Join(" ", Environment.GetCommandLineArgs().Skip(1)) : null);
        }
    }
}

1 个答案:

答案 0 :(得分:6)

您要求用户输入两次。第一个Console.ReadLine();是您要输入“是”或“否”的内容,但是您的if / else if语句要求再次输入

Console.WriteLine("Are You Sure Your User: " + user + " And Pass: " + Pass + " is correct?  Yes/No");
var input = Console.ReadLine();
if (input == "Yes" || input == "yes")
{
   Console.WriteLine("You May now close this");
}
else if(input == "No" || input == "no")
{
   Console.WriteLine("Pleas Press enter");
   System.Diagnostics.Process.Start(Environment.GetCommandLineArgs()[0], Environment.GetCommandLineArgs().Length > 1 ? string.Join(" ", Environment.GetCommandLineArgs().Skip(1)) : null);
}