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

huangapple go评论93阅读模式
英文:

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

问题

这是迄今为止的代码,仅在输入匹配的用户名和密码时(显示欢迎消息)起作用。

我不确定如何集成一个 while 循环,如果 usernameInput 字符串与 username 字符串不匹配,它会阻止用户进入密码阶段,并反复循环,直到输入正确的用户名为止。然后,我希望对于 passwordInput 字符串也是同样的操作,直到它与密码字符串匹配为止。想法是使程序能够一遍又一遍地循环,直到用户输入了正确的用户名和密码。

【注意:我从基本了解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);
        
        while (true) {
            System.out.println("请输入您的用户名:");
            usernameInput = myObj1.nextLine();
            
            if (!usernameInput.equals(username)) {    
                System.out.println("用户名不正确,请重试:");
            } else {
                break; // 退出循环,进入密码阶段
            }
        }
        
        while (true) {
            System.out.println("请输入正确的密码:");
            passwordInput = myObj1.nextLine();
            
            if (!passwordInput.equals(password)) {
                System.out.println("密码不正确,请重试:");
            } else {
                System.out.println("您已成功登录!");
                break; // 登录成功,退出循环
            }
        }
        
        myObj1.close();
    }
}

感谢您的任何帮助!

英文:

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. Then after I would like the same thing for the passwordInput string until it matches the password string. 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]

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!

答案1

得分: 0

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

        } 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!
英文:

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!

答案2

得分: 0

你可以使用 while 循环:

while (someBooleanCondition) {
    statement(s);
}

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

这里有一个可能有用的链接

英文:

You can use a while loop:

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.

Here is a link that may be useful

答案3

得分: 0

你可以在 while 循环内部检查条件时使用赋值运算符:
while (!username.equals(usernameInput = scan.nextLine()))。这样你就不需要额外的布尔变量或者 break/continue 语句来实现必要的流程。

关于使用 Scanner 的附加注释:
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!");
        }
    }
}
英文:

You can use assignment operator when checking the condition inside 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.

Aside comments related to the use of 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. However, using try-with-resources construction guarantees that the resource implementing AutoCloseable interface will be closed automatically.

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

huangapple
  • 本文由 发表于 2020年9月19日 23:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63970145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定