【解決方法】ユーザー名とパスワードでログインする Java プロジェクト

プログラミングQA


登録するかログインするかを選択できるプログラムを作成しようとしましたが、作成者が賢明に選択した場合、登録またはログインのみを選択できることが通知されます…プログラムがループに陥ることがあるという問題がありますこれは、「login」と入力しても間違った入力をしたことを示しており、このループから抜け出すことはできません。 私が得たもう1つの問題は、ログインコードです。ユーザーカウント+1に等しいuser_idを追加し、ログインで取得したユーザーとパスが両方とも正しいかどうかをチェックしたかったので、番号をループするforループを使用しましたユーザーの数を調べて、ユーザー入力がuser_idを持つすべてのユーザーのパスワードと等しいかどうかを確認しますが、その方法がわからないだけで、おそらく任意のユーザーのオブジェクトにカウントを与えると思ったので、ユーザー1を確認できました私のforループuser_id.usernameとuser_id.passwordで1つずつ。

Java
 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

多すぎる while ループし、最後の式が間違っている場合は、次のようにする必要があります。

Java
// 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 ループです。

Java
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);

コメント

タイトルとURLをコピーしました