In: Computer Science
This program does password verification. It gives the user three attempts to type in the correct password. Assume that the password is "COMSC110" (You can hard-code this). If the user types in the correct password, display a message saying "Login successful" and break out of the loop, otherwise display " Login Failed" and go to the next iteration. If the user enters three unsuccessful entries, then display a message saying " Too many attempts, Try again later", Use a while loop.
Java Code:
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String password="COMSC110";
String userInput=new String();
int count=0;
while(count<3){
System.out.print("Please enter password: ");
userInput=sc.nextLine();
if(userInput.equals(password)){
System.out.println("Login Successfull");
break;
}
else{
count++;
System.out.println("Login Failed");
if(count==3){
System.out.println(" Too many attempts, Try again later");
}
}
}
}
}
if you like the answer please provide a thumbs up.