Java If语句和用户名/密码
我希望我的程序提示用户输入设置的用户名和密码。如果凭据不匹配,我希望它循环,直到它们正确执行或超过尝试次数 例如,如果他们得到的“用户名”错误,程序应该继续询问用户的“用户名”,直到他们得到正确的用户名或他们达到5次尝试Java If语句和用户名/密码,java,if-statement,passwords,joptionpane,Java,If Statement,Passwords,Joptionpane,我希望我的程序提示用户输入设置的用户名和密码。如果凭据不匹配,我希望它循环,直到它们正确执行或超过尝试次数 例如,如果他们得到的“用户名”错误,程序应该继续询问用户的“用户名”,直到他们得到正确的用户名或他们达到5次尝试 import javax.swing.JOptionPane; public class Password_DiljotJ_R1 { public static void main(String[] args) { int attempt = 0;
import javax.swing.JOptionPane;
public class Password_DiljotJ_R1 {
public static void main(String[] args) {
int attempt = 0;
String username = "john";
String password = "123";
String usernameEntered;
String passwordEntered;
usernameEntered = (JOptionPane.showInputDialog("Please enter the username"));
passwordEntered = (JOptionPane.showInputDialog("Please enter the password"));
if (usernameEntered.equals(username) && passwordEntered.equals(password) ){
JOptionPane.showMessageDialog(null,"Credentials Match. Welcome John!");
}
else if (usernameEntered.equals(username)) {
JOptionPane.showMessageDialog(null,"Password Invalid.");
attempt++;
passwordEntered = (JOptionPane.showInputDialog("Please enter the password AGAIN"));
}
else if (passwordEntered.equals(password)) {
JOptionPane.showMessageDialog(null, "Username Invalid.");
attempt++;
usernameEntered = (JOptionPane.showInputDialog("Please enter username AGAIN"));
}
else {
JOptionPane.showMessageDialog(null,"Both username and password are inncorrect. Who are you");
attempt++;
usernameEntered = (JOptionPane.showInputDialog("Please enter username AGAIN"));
passwordEntered = (JOptionPane.showInputDialog("Please enter password AGAIN"));
}
if (attempt == 5){
JOptionPane.showMessageDialog(null,"You've reached maximum attempts. Program will now close");
}
}
}
以下代码段将请求用户名和密码,只要给定的用户名和密码与真实用户名和密码不匹配,并且用户只尝试了不到5次
import javax.swing.JOptionPane;
public class Password_DiljotJ_R1 {
public static void main(String[] args) {
int attempt = 0;
String username = "john";
String password = "123";
String usernameEntered;
String passwordEntered;
do {
usernameEntered = (JOptionPane.showInputDialog("Please enter the username"));
passwordEntered = (JOptionPane.showInputDialog("Please enter the password"));
attempt++;
} while (usernameEntered != username && passwordEntered != password && attempt < 5);
}
}
import javax.swing.JOptionPane;
公共类密码_DiljotJ_R1{
公共静态void main(字符串[]args){
int尝试=0;
字符串username=“john”;
字符串密码=“123”;
输入的字符串用户名;
输入字符串密码;
做{
usernameEntered=(JOptionPane.showInputDialog(“请输入用户名”);
passwordEntered=(JOptionPane.showInputDialog(“请输入密码”);
尝试++;
}while(usernameintered!=用户名和密码输入!=密码和尝试<5);
}
}
那么你的问题是什么?您提供的代码有哪些您认为不应该做的事情?我严重怀疑密码应该不区分大小写,除非您是Microsoft使用fixed。多谢各位@基维诺