I want my program to prompt user to enter the set user name and password. If the credentials don't match, I want it to loop until either they get it right or they exceed number of attempts.
For example, if they get "username" wrong, program should keep on asking user of "username" until they get the username right or they reach 5 attempts
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");
}
}
}
Aucun commentaire:
Enregistrer un commentaire