How to modify the code below to become the user only can enter 2 times wrong PIN? After 2 times wrong PIN, the program will auto exit.
String user = "Melissa";
int pin = 123456;
int pin2;
// Prompt the user for input
do
{
String pin2String = JOptionPane.showInputDialog("Enter PIN");
pin2 = Integer.parseInt(pin2String);
}while(pin2 != pin);
// Display
JOptionPane.showMessageDialog(null, "User: "+ user);
You will just need to add a counter, to count how many times the user has attempted to enter a pin, then verify the condition in your
whileloop’s condition.For example: