程序从文本文件中读取用户名和密码

时间:2017-11-19 18:29:40

标签: c# visual-studio c#-4.0

{
        StreamWriter a = new StreamWriter ("qasim.txt");
        a.WriteLine ("user a");
        a.WriteLine ("pass a");
        a.WriteLine ("user b");
        a.WriteLine ("pass b");
        a.Close ();
        string username = Console.ReadLine ();
        string pass = Console.ReadLine ();
        StreamReader ab = new StreamReader ("qasim.txt");

        string line1 = ab.ReadLine ();
        string line2 = ab.ReadLine ();
        //int counter1 = 0;
        //int x = 1;

        while ((line1 != null) && (line2 != null))
        {
            if ((line1 == username) && (line2 == pass))
            {
                Console.WriteLine ("Welcome");
                break;
            }
            else
            {
                Console.WriteLine ("Try Again");
            }

            line1 = ab.ReadLine ();
            line2 = ab.ReadLine ();
        }
        Console.WriteLine ();


        ab.Close ();
    }

我必须编写一个程序,从用户那里获取用户名和密码,并从文本文件中检查它们,如果值是正确的,则显示“欢迎”,如果值不正确则显示“再试一次”。我在给定代码中遇到的问题是,如果输入错误的用户名和密码,输出将“再次尝试”两次。

2 个答案:

答案 0 :(得分:0)

您可以在if。

下添加Console.WriteLine("Try Again");
{
    StreamWriter a = new StreamWriter ("qasim.txt");
    a.WriteLine ("user a");
    a.WriteLine ("pass a");
    a.WriteLine ("user b");
    a.WriteLine ("pass b");
    a.Close ();
    string username = Console.ReadLine ();
    string pass = Console.ReadLine ();
    StreamReader ab = new StreamReader ("qasim.txt");

    string line1 = ab.ReadLine ();
    string line2 = ab.ReadLine ();
    //int counter1 = 0;
    //int x = 1;

    while ((line1 != null) && (line2 != null))
    {
        if ((line1 == username) && (line2 == pass))
        {
            Console.WriteLine ("Welcome");
            break;
        }
        Console.WriteLine ("Try Again");

        username = Console.ReadLine ();
        pass = Console.ReadLine ();
    }
    Console.WriteLine ();


    ab.Close ();
}

或者在else语句中:

{
    StreamWriter a = new StreamWriter ("qasim.txt");
    a.WriteLine ("user a");
    a.WriteLine ("pass a");
    a.WriteLine ("user b");
    a.WriteLine ("pass b");
    a.Close ();
    string username = Console.ReadLine ();
    string pass = Console.ReadLine ();
    StreamReader ab = new StreamReader ("qasim.txt");

    string line1 = ab.ReadLine ();
    string line2 = ab.ReadLine ();
    //int counter1 = 0;
    //int x = 1;

    while ((line1 != null) && (line2 != null))
    {
        if ((line1 == username) && (line2 == pass))
        {
            Console.WriteLine ("Welcome");
            break;
        }
        else
        {
            Console.WriteLine ("Try Again");
        }

        username = Console.ReadLine ();
        pass = Console.ReadLine ();
    }
    Console.WriteLine ();


    ab.Close ();
}

这样程序会再次询问用户密码,直到用户输入正确的用户名/密码。

答案 1 :(得分:0)

public static void Main (string[] args)
    {
        //I am using StreamWriter here only to show//
        //the file can contain more lines//
        StreamWriter a = new StreamWriter ("qasim.txt");
        a.WriteLine ("user1");
        a.WriteLine ("@abc");
        a.WriteLine ("user2");
        a.WriteLine ("abc@");
        a.Close ();

        Console.WriteLine ("Kindly Enter Username:");
        string username = Console.ReadLine();

        Console.WriteLine ("Kindly Enter Password:");
        string pass = Console.ReadLine ();


        StreamReader ab = new StreamReader ("qasim.txt");
        bool userExist = false;
        string line1 = ab.ReadLine();
        string line2 = ab.ReadLine();

        while ((line1 != null) && (line2 != null))
        {
            if ((line1 == username) && (line2 == pass)) 
            {
                userExist = true;
                Console.WriteLine ("Welcome");
                break;
            } 

            line1 = ab.ReadLine ();
            line2 = ab.ReadLine ();
        }
        if (userExist == false) 
        {
            Console.WriteLine ("Try Again");
        }


        ab.Close ();
    }

现在程序工作程序工作正常。