C#登录/注册简单控制台应用程序

时间:2019-03-13 23:03:09

标签: c# console-application

当我创建用户时,它存储在字符串中,但是当我尝试使用相同的用户名登录时(以某种方式在字符串中不存在...)

class UserModelChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
         return obj.get_full_name()

class TaskForm(forms.ModelForm):
    worker = UserModelChoiceField(queryset=User.objects.filter(is_active=1).order_by('first_name'),widget=forms.Select(attrs={'class':'form-control'}),required=False)
    class Meta:
        model = Task
        fields = ['worker']

不能弄清楚怎么做。

1 个答案:

答案 0 :(得分:0)

class Program
{
    static void Main(string[] args)
    {
        var arrUsers = new Users[]
        {
            new Users("tomas","samsung",2605),
            new Users("stefan","pasle",15),
            new Users("dimitar","jovanov",32)
        };

        Start:
        Console.WriteLine("Za login stisnete 1 ili za register 2");
        var input = Console.ReadLine();



        bool successfull = false;
        while (!successfull)
        {

            if (input == "1")
            {
                Console.WriteLine("Write your username:");
                var username = Console.ReadLine();
                Console.WriteLine("Enter your password:");
                var password = Console.ReadLine();


                foreach (Users user in arrUsers)
                {
                    if (username == user.username && password == user.password)
                    {
                        Console.WriteLine("You have successfully logged in !!!");
                        Console.ReadLine();
                        successfull = true;
                        break;
                    }
                }

                if (!successfull)
                {
                    Console.WriteLine("Your username or password is incorect, try again !!!");
                }

            }

            else if (input == "2")
            {

                Console.WriteLine("Enter your username:");
                var username = Console.ReadLine();

                Console.WriteLine("Enter your password:");
                var password = Console.ReadLine();

                Console.WriteLine("Enter your id:");
                int id = int.Parse(Console.ReadLine());


                Array.Resize(ref arrUsers, arrUsers.Length + 1);
                arrUsers[arrUsers.Length - 1] = new Users(username, password, id);
                successfull = true;
                goto Start;

            }
            else
            {
                Console.WriteLine("Try again !!!");
                break;


            }

        }

    }
}

public class Users
{
    public string username;
    public string password;
    private int id;

    public Users(string username, string password, int id)
    {
        this.username = username;
        this.password = password;
        this.id = id;
    }
}

我对您的代码进行了一些更改,请检查差异

我将arrUsers放置在Start之前,以便您转到Start时引用不会更改