将用户输入与数组进行比较

时间:2017-03-09 03:17:31

标签: c# arrays passwords username

我刚刚在我的学校开始使用C#(这不是家庭作业),我是这个网站的新手。我正在尝试制作我自己的程序,它将接受用户输入(或用户名和密码),然后将用户输入与当前只有一行的数组进行比较。这是我代码的一部分...

 int count = 0;
        char delimiter = '#';
        string fileInput = "";
        string[] inputArray;

        fileInput = inFile.ReadLine();
        while(fileInput != null)
        {
            inputArray = fileInput.Split(delimiter);
            username[count] = inputArray[0];
            password[count] = inputArray[1];

/////////////

static void compare(string[] username, string[] password, int count, string userInputUsername, string userInputPassword)
{
    userInputUsername = userInputUsername.ToLower();

    for (int x = 0; x < count; x++)
    {
        if (userInputUsername == username[x] && userInputPassword == password[x]) 
        {
            loginSuccesful();                   
        }
        else if (userInputUsername != username[x] || userInputPassword != password[x])
        {
            loginFailed(); // Seems to give the login failed message multiple times based on the # of lines in an array in the login.txt document.
        }
    }
}

我的数组看起来像这样,TJ#1(TJ是用户名,1是密码)。当我要求它时,它正确地打印出密码和用户名,但每当我拿到我的userInputUsername并将它与数组进行比较时,它总是给我我的loginFailed()方法。我有userInputUsername和Array作为字符串

2 个答案:

答案 0 :(得分:0)

我还没有能够摆弄这个,但我认为我遇到的问题是由于我试图用一种方法回归两件事。

答案 1 :(得分:-1)

我认为你应该使用&#34; equals&#34;比较,像这样:

import csv
import itertools
from itertools import *
import json


def read_with_header():
    with open ('/Users/bhargavsaidama/test.csv', 'rb') as f:
        reader = csv.reader(f, delimiter = ',', quotechar = '|')
        row_count = 0
        keys = []
        for row in reader:
            row_count = row_count + 1
            keys.append(row)
        header = keys[0]
        return row_count, header


def reading_ignore_header():

    row_count, header = read_with_header()

    with open('/Users/bhargavsaidama/test.csv', 'rb') as f:
        f.next()
        # row_count = sum(1 for row in f)
        # # print row_count
        reader = csv.reader(f, delimiter = ',' , quotechar = '|')
        result = []
        values = ()

        for row in reader:
            # row_count is taken including header file in the above function
            values = tuple((itertools.combinations(row, 2)))[:(row_count-1)]  # row_count is important,since your keys are rows

            for x, y in values:
                result.append({x:y})
        return result, header

# The following function is taken from here
# http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]


def main():

    result, header = reading_ignore_header()
    final_values = list(chunks(result,3)) # here 3 reflects (row_count-1)
    header = header[1:]        # seems like u wanna ignore Id 
    data_str = json.dumps(dict(zip(header, final_values)))
    data_json = json.loads(data_str)
    print data_str, data_json
    return data_str, data_json


if __name__ == "__main__":
    main()