简体   繁体   English

需要帮助在我的 Java 程序中集成一个 while 循环。 希望程序更好地处理错误输入

[英]Need help intgerating a while loop in my java program. Want the program to handle incorrect input better

This is my code so far that only works (displays the welcome message) when both a matching username and password have been entered.到目前为止,这是我的代码,只有在输入了匹配的用户名和密码时才有效(显示欢迎消息)。

I am unsure how to integrate a while loop that stops the user from progressing to the password stage if the usernameInput string does not match the username string, and loops over and over until the correct username has been entered.如果 usernameInput 字符串与用户名字符串不匹配,我不确定如何集成一个 while 循环来阻止用户进入密码阶段,并一遍又一遍地循环直到输入正确的用户名。 Then after I would like the same thing for the passwordInput string until it matches the password string.然后我想对 passwordInput 字符串做同样的事情,直到它与密码字符串匹配。 The idea is to have the program be able to loop over and over until the correct username AND password have been entered by the user.这个想法是让程序能够一遍又一遍地循环,直到用户输入正确的用户名和密码。

[NOTE: I'm coming into java from a very basic knowledge of python, so everything may be a little disordered and inefficient] [注意:我是从 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();
    }

}

Thanks for any help!谢谢你的帮助!

Can you please check if are you looking for something like this?你能检查一下你是否在寻找这样的东西吗?

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();
            }
        }

    }
}

Output:输出:

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!

You can use a while loop:您可以使用 while 循环:

while (someBooleanCondition) {
    statement(s);
}

When both conditions are true (password and username) then the boolean condition breaks the while loop and tells the user they have successfully logged in.当两个条件都为真(密码和用户名)时,布尔条件会中断 while 循环并告诉用户他们已成功登录。

Here is a link that may be useful 这是一个可能有用的链接

You can use assignment operator when checking the condition inside while : while (!username.equals(usernameInput = scan.nextLine())) .您可以在检查while内的条件时使用赋值运算符: while (!username.equals(usernameInput = scan.nextLine())) Then you won't need any extra boolean variables and/or break / continue statements to implement necessary flow.那么您将不需要任何额外的布尔变量和/或break / continue语句来实现必要的流程。

Aside comments related to the use of Scanner :除了与使用Scanner相关的评论:

  1. there's no need to create a separate instance to input username and password.无需创建单独的实例来输入用户名和密码。
  2. It's good to see that you cared for closing both Scanner instances.很高兴看到您关心关闭两个Scanner实例。 However, using try-with-resources construction guarantees that the resource implementing AutoCloseable interface will be closed automatically.但是,使用try-with-resources构造可以保证实现AutoCloseable接口的资源将自动关闭。

That being said, the code may be rewritten as follows:话虽如此,代码可以改写如下:

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!");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM