我試圖構建一個簡單的用戶界面,用戶輸入用戶名,如果用戶名是 xyz,那么用戶將顯示“輸入您的密碼”。如果密碼是 xyz1234,那么用戶將顯示“請等待加載...”并且掃描儀退出。如果用戶名不正確,我嘗試以某種方式對其進行編碼,以便它不斷詢問用戶名,直到輸入正確的用戶名。
顯然,代碼的第一部分有效,如果用戶名是 xyz,那么代碼可以正常作業并顯示我想要的內容。但是,如果用戶名在第一次嘗試時是錯誤的,而在第二次嘗試時是正確的,它仍然會繼續顯示“用戶名錯誤”而不是顯示“輸入密碼”。
代碼如下所示:
import java.util.Scanner;
class User_Authentication
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter your Username");
String username=in.nextLine();
if(username.equals("xyz"))
{
System.out.println("Enter the Password");
String password=in.nextLine();
if(password.equals("xyz1234"))
{
System.exit(0);
System.out.println("Welcome");
System.out.println("Please wait while the system is loading....");
}
else
{
System.out.println("Your system has been locked for security reasons.");
System.out.println("Please contact an administrator to reset the password");
System.out.println("Any attempt to break in the computer will result in self destruction.");
System.exit(0);
}
}
else
{
do
{
if(username.equals("xyz"))
{
break;
}
System.out.println("Incorrect username");
System.out.println("Please try again");
in.nextLine();
}
while((username.equals("xyz"))==false);
}
}
} ```
uj5u.com熱心網友回復:
您在 do-while 回圈中的說法in.nextLine(),而不是username = in.nextLine()
替換它,它應該可以作業,但總體而言,你是 do-while 回圈需要一些作業,它相對混亂。這是我將如何處理它。
do {
System.out.println("Incorrect username");
System.out.println("Please try again");
username = in.nextLine();
} while(username.equals("xyz") == false);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511684.html
標籤:爪哇验证用户界面密码保护
