Skip to content
Advertisement

how to use IF else inside Do while loop [closed]

I want to Make a game Enter ATM pin with only 5 chances if you guess the correct pin it will stop automatic, and if Chances = 0 it will stop also,

int answer, chances = 5;

 do{
     Scanner scan = new Scanner(System.in);
     System.out.print("Enter your Bank ATM pin : ");       
     answer= scan.nextInt();
             chances--;
      
             
 }while(chances > 0);
 
}

Advertisement

Answer

You can do something like that:

int answer, chances = 5;
boolean authorized=false
do{
     Scanner scan = new Scanner(System.in);
     System.out.print("Enter your Bank ATM pin : ");       
     answer= scan.nextInt();
     if(answer.equals("ok")){
        authorized=true;
     }else{
         chances--;
     }  
 }while(chances > 0 || authorized);

Code Safe

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement