学习Java和For循环不是渲染
我在下面创建了这个学习Java和For循环不是渲染,java,Java,我在下面创建了这个PassworField方法。其目的是在用户输入正确的密码时打破循环。它应该在阻塞用户之前循环5次。如果用户第一次输入正确的密码,则会发生错误,但如果用户先输入错误的密码,然后在第二次尝试时更正,则不会发生错误。它只是循环5次,即使我输入正确的密码,然后阻塞。如果你能帮我,我会很感激的…让我发疯 public class LoginPage { static String password = "hello"; static Scanne
PassworField
方法。其目的是在用户输入正确的密码时打破循环。它应该在阻塞用户之前循环5次。如果用户第一次输入正确的密码,则会发生错误,但如果用户先输入错误的密码,然后在第二次尝试时更正,则不会发生错误。它只是循环5次,即使我输入正确的密码,然后阻塞。如果你能帮我,我会很感激的…让我发疯
public class LoginPage {
static String password = "hello";
static Scanner scn = new Scanner(System.in);
static String input = scn.nextLine();
public static void PasswordField() {
System.out.println("Enter the password");
for (int i = 0; i <5; i++) {
if (password.equals(input)) {
System.out.println("You are In");
break;
}
else if(!input.equals(password)) {
System.out.println("Wrong, Please Enter the Password again:");
scn.nextLine();
}
else {
System.out.println("You are Blocked sucker. call helpdesk");
}
}
scn.close();
}
公共类登录页{
静态字符串password=“hello”;
静态扫描仪scn=新扫描仪(System.in);
静态字符串输入=scn.nextLine();
公共静态无效密码字段(){
System.out.println(“输入密码”);
for(int i=0;i缺少的是用新值更新输入
。此外,被阻止的逻辑应该在for循环完成之后
公共静态无效密码字段(){
System.out.println(“输入密码”);
对于(int i=0;i<5;i++){
if(密码等于(输入)){
System.out.println(“您在”);
scn.close();
回来
}否则{
System.out.println(“错误,请重新输入密码:”);
输入=scn.nextLine();
}
}
System.out.println(“你被阻止了,笨蛋,打电话给服务台”);
scn.close();
}
一旦循环终止,您需要检查用户是否超过了允许的最大尝试次数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
inputPassword();
}
public static void inputPassword() {
final Scanner scn = new Scanner(System.in);
final String password = "hello";
String input;
int i;
for (i = 1; i <= 5; i++) {
System.out.print("Enter the password: ");
input = scn.nextLine();
if (password.equals(input)) {
System.out.println("You are In");
break;
} else if (i < 5) {
System.out.println("Wrong, Please Enter the Password again.");
}
}
if (i > 5) {
System.out.println("You are blocked as you have exceeded the maximum limit.");
}
}
}
另一个示例运行:
Enter the password: 1
Wrong, Please Enter the Password again.
Enter the password: 2
Wrong, Please Enter the Password again.
Enter the password: 3
Wrong, Please Enter the Password again.
Enter the password: 4
Wrong, Please Enter the Password again.
Enter the password: 5
You are blocked as you have exceeded the maximum limit.
Enter the password: 1
Wrong, Please Enter the Password again.
Enter the password: Hello
Wrong, Please Enter the Password again.
Enter the password: hello
You are In
其他一些重要注意事项:
Enter the password: 1
Wrong, Please Enter the Password again.
Enter the password: 2
Wrong, Please Enter the Password again.
Enter the password: 3
Wrong, Please Enter the Password again.
Enter the password: 4
Wrong, Please Enter the Password again.
Enter the password: 5
You are blocked as you have exceeded the maximum limit.
Enter the password: 1
Wrong, Please Enter the Password again.
Enter the password: Hello
Wrong, Please Enter the Password again.
Enter the password: hello
You are In
不要关闭系统中的扫描仪
,因为它也会关闭系统中的
例如,按照命名约定,按照方法,PasswordField
应命名为PasswordField
将“readLine”之类的阻塞方法放在静态字段上是一个可怕的想法。至少让该方法的输入成为本地的。我喜欢这个答案。但由于某些原因,第一个系统输出“输入密码”在我运行时不会打印。它只在我输入后打印…@RahenCHoudhury第一次调用scanner.nextLine()在初始化类时发生,因为您在字段声明中调用它。请在打印该行后进行调用。我这样做了,但仍然不起作用。请查看下面的代码,这是什么意思?@JCOC611 public static void PasswordField(){System.out.println(“输入密码”);for(int I=0;I