【问题标题】:If statement running both conditions, even though its IMPOSSIBLE to run the second condition if the first is met c#如果语句运行两个条件,即使如果满足第一个条件,它不可能运行第二个条件 c#
【发布时间】:2018-02-20 18:03:29
【问题描述】:

我有一些代码,用于登录我正在编写的某些软件,代码会根据包含用户名和散列密码的文件检查用户名和密码,但是,如果我输入任何其他登录名而不是存储在在文件的第一行,if 语句的两个部分都将运行,即使如果满足第一个条件,“else”就不可能运行。

我似乎找不到代码有任何问题,当然,如果登录详细信息正确,它会登录用户,即使它给出的错误是他们无法登录。

代码:

System.IO.StreamReader file = new System.IO.StreamReader("UserData.txt");
string fileLine;
string line;
while ((line = file.ReadLine()) !=null)
{
    fileLine = line;
    System.Diagnostics.Debug.WriteLine(fileLine);
    string len = fileLine.Length.ToString();
    string usr = fileLine.Substring(0, 20);
    string hash = fileLine.Substring(20, 32);
    if (usr.Contains(UserNameIpt.Text) && hash == password)
    {
        GlobalVar.UserNameEntered = UserNameIpt.Text;
        showUserPanel();
        break;
    }
    else
    {
        MessageBox.Show("Error, password or username incorrect. \r\n\r\nyou may not have an account set up to use this software, please contact the system administartor for assistance.", "Login Error");
    }
}

【问题讨论】:

  • 您是否尝试过在调试器中单步执行此代码?我很难相信代码会在 while 循环的同一迭代中执行 ifelse
  • 问题是你会得到文件中每一行的错误——而不是到最后,所以如果你列出了 100 个用户,你会得到 100 个消息框.....
  • 是的,我想这更多的是与 while 而不是 if。

标签: c# if-statement conditional-statements


【解决方案1】:

让我用伪代码向你解释一下你的代码

假设您的文件仅包含名称(以缩短寿命)假设它有

jon
kate
alison
fred
bill
  • 所以你的代码说给我一个名字,我输入 pete

  • 然后您的代码打开文件并读取 jon 并出现“错误”

  • 读取 kate 出现“错误”

  • 读取艾莉森“错误”

    ...

所以

你需要稍微改变你的代码

  • 选择一个标记为“找到”并将其设置为 false

  • 如果我这次选kate,

  • 读取 jon - 什么都不做

  • 读取 kate - 找到并中断的集合

  • 如果找到则ok - 否则说“错误”

如果我选择了 pete,它会到达文件的末尾,发现是假的,它只会说一次“错误”

【讨论】:

  • 啊,可能是这样,我试试看能不能用
【解决方案2】:

所以,我实际上设法解决了它,而不是使用isFound条件,我使用了嵌套的'if',将'break'替换为return,在显示错误后添加它,并添加要在之后显示的错误while 语句运行,到目前为止,这似乎是有效的,尽管它可能不是最有效的方法。

            System.IO.StreamReader file = new System.IO.StreamReader("UserData.txt");
            string fileLine;
            string line;
            bool isFound = false;
            while ((line = file.ReadLine()) !=null)
            {


                fileLine = line;
                System.Diagnostics.Debug.WriteLine(fileLine);
                string len = fileLine.Length.ToString();
                string usr = fileLine.Substring(0, 20);
                string hash = fileLine.Substring(20, 32);
                if (usr.Contains(UserNameIpt.Text))
                {
                    if (hash == password)
                    {
                        GlobalVar.UserNameEntered = UserNameIpt.Text;
                        showUserPanel();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error, password or username incorrect. \r\n\r\nyou may not have an account set up to use this software, please contact the system administartor for assistance.", "Login Error");
                        return;
                    }

                }


            }
            MessageBox.Show("Error, password or username incorrect. \r\n\r\nyou may not have an account set up to use this software, please contact the system administartor for assistance.", "Login Error");

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多