Eran Daniel Ответов: 1

Имя пользователя и пароль входа в java проект


I tried to write a program that lets you choose if you want to register or login and if chose author wise it will let you know that you can only choose register or login... I have a problem that the program sometimes stuck in the loop which tells me that I have entered the wrong input although I type "login" and you cannot get out of this loop. one more problem I got is the login code: I added user_id who is equal to the user count+1 and I wanted to check if the user and pass that I got in the login are both correct with a for loop which loops over the number of users and check if the user input is equal to the password of every user with the user_id and I just don't know how to do it I thought maybe to give the object of any user with the count and so I could check user one by one in my for loop user_id.username and user_id.password.

 java.util.Scanner;

public class users {
    public String user_name;
    public int user_id = 1;
    private String password;
    public static int count = 1;
    public static String input;

    public users(String Ruser, String Rpassword) {

        this.user_id = count++;
        this.user_name = Ruser;
        this.password = Rpassword;
        count++;

        System.out.printf("User %s has been crated \n", Ruser);
        System.out.printf("Enter 'login' to log in or 'register' to open another account");

    }

    public static void login(String Luser, String Lpassword) {
        for (int i = 1; i <= count; i++) {
            System.out.printf("Enter 'login' to log in or 'register' to open another account");
            // user_id.users
            // if(this.user_name)
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("login");
        System.out.println("register");
        input = scanner.nextLine();


            while (input.equals("login")) {

                System.out.println("username");
                String Luser = scanner.nextLine();
                System.out.println("Password");
                String Lpassword = scanner.nextLine();
                int a = count;
                login(Luser, Lpassword);
                System.out.println("");
                input = scanner.nextLine();
            }
            while (input.equals("register")) {

                System.out.println("username");
                String Ruser = scanner.nextLine();
                System.out.println("Password");
                String Rpassword = scanner.nextLine();
                users count = new users(Ruser, Rpassword);
                System.out.println("");
                input = scanner.nextLine();
            }
            while ((!input.equals("register")) || (!input.equals("login"))) {
                System.out.println("invild option, chose login or regiser!");
                input = scanner.nextLine();


        }
<pre>
импортировать

Что я уже пробовал:

я пытался возиться с кодом в течение нескольких часов..

1 Ответов

Рейтинг:
2

Richard MacCutchan

Слишком много while петли, и выражение на последнем из них неверно, оно должно быть:

// use && as both expressions need to be true.
            while ((!input.equals("register")) && (!input.equals("login"))) {
                System.out.println("invild option, chose login or regiser!");
                input = scanner.nextLine();

Лучшим способом был бы один цикл do/while, что-то вроде:
do
{
    System.out.println("Enter \"login\", \"register\", or \"exit\"");
    input = scanner.nextLine();
    if (input.equals("login")
    {
        // get login details
    }
    else if (input.equals("register")
    {
        // get register details
    }
    else if (input.equals("exit")
    {
        break; // exit the loop
    }
    else
    {
        // invalid input, tell them to try again
    }
} while (true);