import java.util.Scanner; public class Main { static Scanner kbin = new Scanner(System.in); public static final String RESET = "\u001B[0m"; public static final String RED = "\u001B[31m"; public static final String GREEN = "\u001B[32m"; public static final String YELLOW = "\u001B[33m"; public static void main(String[] args) { System.out.println("Logging in! You have 3 chances\n\n"); String password = new String("5ecret!"); //doWhileSample(password); //whileSample(password); //forloopSample(password); } public static void doWhileSample(String pWord) { int count = 0; String myPassword; do { System.out.println(GREEN+"You have "+(3-count)+" chances to enter the password"+RESET); myPassword = kbin.nextLine(); count++; }while(!myPassword.equals(pWord) && count<3); //how did we get out of the loop? if(myPassword.equals(pWord)) { System.out.println("Logging in..."); }else { System.out.println(RED+"Your account has been locked."+RESET); } } public static void whileSample(String pWord) { int count = 0; String myPassword; System.out.println("You have "+(3-count)+" chances to enter the password"); myPassword = kbin.nextLine(); count++; while(!myPassword.equals(pWord) && count<3){ System.out.println("You have "+(3-count)+" chances to enter the password"); myPassword = kbin.nextLine(); count++; }; //how did we get out of the loop? if(myPassword.equals(pWord)) { System.out.println("Logging in..."); }else { System.out.println(RED+"Your account has been locked."+RESET); } } public static void forloopSample(String pWord) { String myPassword; for(int count = 0; count<3; count++){ System.out.println("You have "+(3-count)+" chances to enter the password"); myPassword = kbin.nextLine(); if(myPassword.equals(pWord)) { break; } } //how did we get out of the loop? if(myPassword.equals(pWord)) { System.out.println("Logging in..."); }else { System.out.println(RED+"Your account has been locked."+RESET); } } }