如何在Java登录系统中创建循环?用户应具有;“3次尝试”;登入

如何在Java登录系统中创建循环?用户应具有;“3次尝试”;登入,java,loops,login,Java,Loops,Login,我有一个任务,我需要用Java创建一个登录系统(我使用Eclipse作为IDE)。 我的登录系统工作正常,但我缺少一个循环,用户可以从中“重试”。我真的很想如果用户可以有3次尝试 有可能吗?有人能帮我吗 这是我的代码: package BookingSystemPackage; import java.util.Scanner; public class Booking_login { public static void main(String[] args) { Scann

我有一个任务,我需要用Java创建一个登录系统(我使用Eclipse作为IDE)。 我的登录系统工作正常,但我缺少一个循环,用户可以从中“重试”。我真的很想如果用户可以有3次尝试

有可能吗?有人能帮我吗

这是我的代码:

package BookingSystemPackage;

import java.util.Scanner;

public class Booking_login {

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    System.out.println("Enter your Social Security number in the format DDMMYY-XXXX: "); 
    String cpr; 
    cpr = input.next();

    if (cpr.length() != 11 || cpr.charAt(6) != '-') {         
        System.out.println(cpr + " You have entered a invalid social security number");
    } 

    else if (Character.isDigit(cpr.charAt(0)) 

        == false || Character.isDigit(cpr.charAt(1)) 
        == false || Character.isDigit(cpr.charAt(2)) 
        == false || Character.isDigit(cpr.charAt(3)) 
        == false || Character.isDigit(cpr.charAt(4)) 
        == false || Character.isDigit(cpr.charAt(5)) 
        == false || Character.isDigit(cpr.charAt(7)) 
        == false || Character.isDigit(cpr.charAt(8)) 
        == false || Character.isDigit(cpr.charAt(9)) 
        == false || Character.isDigit(cpr.charAt(10)) 
        == false) { 

        System.out.println("You have entered a invalid social security number");

    } 

    else { 

        System.out.println("You have entered a valid social security number");

        // ----------------------------------------------------------------------------------------

        Scanner inputLogin = new Scanner(System.in);
        System.out.println("Please write your user name: "); 
        String UserName = inputLogin.nextLine(); 
        System.out.println("Please write your password: "); 
        String Password = inputLogin.next();

        System.out.println((validateUser(UserName,Password)) ? "You are logged in" : "You are not logged in"
    }
}


public static boolean validateUser(String UserName, String Password) { 
    String[] userNames = {"Super user", "User", "Admin", "Lucille"}; 
    String[] passwords = {"AA11" , "BB22" , "CC33", "HVORFOR!"}; 
    boolean chek = false; 
    for (int i = 0; i < userNames.length; i++) { 
        if (UserName.equals(userNames[i])) { 
            if (Password.equals(passwords [i])) { chek = true; } 
        }  
    }
    return chek;
}
package-BookingSystemPackage;
导入java.util.Scanner;
公共课堂预约(登入){
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“以DDMMYY-XXXX:”格式输入您的社会保险号);
弦式心肺复苏术;
cpr=input.next();
如果(cpr.length()!=11 | | cpr.charAt(6)!='-'){
System.out.println(cpr+“您输入的社会保险号码无效”);
} 
else if(字符isDigit(cpr.charAt(0))
==false | |字符.isDigit(cpr.charAt(1))
==false | | Character.isDigit(cpr.charAt(2))
==false | | Character.isDigit(cpr.charAt(3))
==false | | Character.isDigit(cpr.charAt(4))
==false | | Character.isDigit(cpr.charAt(5))
==false | | Character.isDigit(cpr.charAt(7))
==false | | Character.isDigit(cpr.charAt(8))
==false | | Character.isDigit(cpr.charAt(9))
==false | | Character.isDigit(cpr.charAt(10))
==假){
System.out.println(“您输入的社会保险号码无效”);
} 
否则{
System.out.println(“您已输入有效的社会保险号码”);
// ----------------------------------------------------------------------------------------
扫描仪输入登录=新扫描仪(System.in);
System.out.println(“请写下您的用户名:”);
字符串UserName=inputLogin.nextLine();
System.out.println(“请写下您的密码:”);
字符串密码=inputLogin.next();
System.out.println((validateUser(用户名、密码))?“您已登录”:“您未登录”
}
}
公共静态布尔validateUser(字符串用户名、字符串密码){
字符串[]用户名={“超级用户”、“用户”、“管理员”、“露西尔”};
字符串[]密码={“AA11”、“BB22”、“CC33”、“HVORFOR!”};
布尔chek=false;
对于(inti=0;i
这应该给你一个想法:

int i = 0;
boolean isLoggedIn = false;
String password = "123";

while (!isLoggedIn) {
   System.out.print("Please enter your password: ");
   String input = new Scanner(System.in).next();

   if (input.equals(password)) { 
      System.out.println("Logged in!");
      isLoggedIn = true; 
   } else {
      if (i == 2) {
         System.out.println("You have tried too many times. System will now shut down.");
         System.exit(0);
      }
      System.out.println("Not logged in");
      i += 1;
   }
}

我会将密码检查分解为一个单独的方法,该方法返回
true
false
,具体取决于用户是否成功登录。然后调用该方法并根据返回值采取适当的操作。因此,主方法可能有以下内容

String password = "s3cr3t";
Scanner input = new Scanner(System.in);
boolean authenticated = login(password, 3, input);
if (authenticated) {
    // Do some stuff here
} else {
    System.out.println("Sorry, I can't let you do that");
}
您的登录方法可能如下所示

public static boolean login(String password, int numberOfAttempts, Scanner input) {
    for (int attempt = 1; attempt <= numberOfAttempts; attempt++) {
        System.out.format("Attempt %d - Please type your password: ", attempt);
        String passwordEntered = input.nextLine();
        if (passwordEntered.equals(password)) {
            return true;
        }
    }
    return false;
}
publicstaticboolean登录(字符串密码、整数尝试次数、扫描仪输入){

对于(int-trunt=1;trunt您尝试了什么?提示:存储每个用户的尝试次数,并在成功登录时将其重置。