【问题标题】:Need help intgerating a while loop in my java program. Want the program to handle incorrect input better需要帮助在我的 java 程序中集成一个 while 循环。希望程序更好地处理错误输入
【发布时间】:2020-09-19 15:01:28
【问题描述】:

到目前为止,这是我的代码,只有在输入了匹配的用户名和密码时才有效(显示欢迎消息)。

我不确定如何集成一个 while 循环,如果 usernameInput 字符串与用户名字符串不匹配,该循环会阻止用户进入密码阶段,并反复循环直到输入正确的用户名。然后在我想要密码输入字符串相同的东西之后,直到它与密码字符串匹配。这个想法是让程序能够一遍又一遍地循环,直到用户输入了正确的用户名和密码。

[注意:我是从非常基础的python知识进入java的,所以一切可能有点混乱和低效]

import java.util.Scanner;

public class Login {

    public static void main(String[] args) {
        
        String username,usernameInput, password, passwordInput;
        username= "KenBoneL0ver";
        password= "KenBone4Life";
        
        Scanner myObj1= new Scanner(System.in);
        
        System.out.println("Please enter your username:");
        usernameInput= myObj1.nextLine();
        
        if (!usernameInput.equals(username)) {    
            System.out.println("Incorrect username. Please try again:");
        }
        else {
            System.out.println("Now please enter the correct password:");
        }
        
        Scanner myObj2= new Scanner(System.in);
        
        passwordInput= myObj2.nextLine();
        
        if (!passwordInput.equals(password)) {
            System.out.println("Incorrect password. Please try again:");
        }
        else {
            System.out.println("You have successfully logged in!"); 
        }
        
        myObj1.close();
        myObj2.close();
    }

}

感谢您的帮助!

【问题讨论】:

  • 创建一个当布尔变量为假时继续的while循环,然后在给出正确的用户名和密码后立即在循环内将该变量设置为真。

标签: java while-loop


【解决方案1】:

请问您是否正在寻找类似的东西?

import java.util.Scanner;

public class Login {

    public static void main(String[] args) {
        Scanner myObj1 = null;
        try {
            String username, usernameInput, password, passwordInput;
            username = "KenBoneL0ver";
            password = "KenBone4Life";

            myObj1 = new Scanner(System.in);

            while (true) {
                System.out.println("Please enter your username:");
                usernameInput = myObj1.nextLine();
                if (!usernameInput.equals(username)) {
                    System.out.println("Incorrect username. Please try again:");
                    continue;
                }
                break;
            }

            while (true) {
                // Scanner myObj2 = new Scanner(System.in);
                System.out.println("Now please enter the correct password:");
                passwordInput = myObj1.nextLine();

                if (!passwordInput.equals(password)) {
                    System.out.println("Incorrect password. Please try again:");
                    continue;
                } else {
                    System.out.println("You have successfully logged in!");
                    break;
                }
            }

            // myObj2.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (myObj1 != null) {
                myObj1.close();
            }
        }

    }
}

输出:

Please enter your username:
test
Incorrect username. Please try again:
Please enter your username:
KenBoneL0ver
Now please enter the correct password:
test
Incorrect password. Please try again:
Now please enter the correct password:
KenBone4Life
You have successfully logged in!

【讨论】:

  • 我的主要目标是复制我过去在 python 中所做的一些事情,但是在查找对此的支持之后,我很难理解 java 处理用户输入的方式。但是,您的编辑正是我想要做的。非常感谢你。你不知道有什么适合初学者的好资源,其中解释了“try”、“continue”、“break”关键字?
【解决方案2】:

你可以使用while循环:

while (someBooleanCondition) {
    statement(s);
}

当两个条件都为真(密码和用户名)时,布尔条件会中断 while 循环并告诉用户他们已成功登录。

Here is a link that may be useful

【讨论】:

    【解决方案3】:

    检查while内的条件时可以使用赋值运算符: while (!username.equals(usernameInput = scan.nextLine()))。那么您将不需要任何额外的布尔变量和/或break/continue 语句来实现必要的流程。

    除了与Scanner的使用相关的cmets:

    1. 无需创建单独的实例来输入用户名和密码。
    2. 很高兴看到您关心关闭两个Scanner 实例。但是使用try-with-resources构造保证实现AutoCloseable接口的资源会自动关闭。

    话虽如此,代码可以改写如下:

    import java.util.Scanner;
    
    public class Login {
    
        public static void main(String[] args) {
            
            String 
                username = "user",
                password = "pswd",
                usernameInput, 
                passwordInput;
            
            try (Scanner scan = new Scanner(System.in)) {
                System.out.println("Please enter your username:");
                while(!username.equals(usernameInput = scan.nextLine())) {
                    System.out.println("Incorrect username. Please try again:");
                }
                
                System.out.println("Now please enter the correct password:");
                while(!password.equals(passwordInput = scan.nextLine())) {
                    System.out.println("Incorrect password. Please try again:");
                }
                System.out.println("You have successfully logged in!");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 2013-05-24
      • 1970-01-01
      相关资源
      最近更新 更多