samedi 13 novembre 2021

Why doesn't the if condition inside the for loop doesn't run?

The code basically generates a Pincode and asks the user to write it. Then displays whether the input is correct or not. Also, if the user got it right after one or two trials it should display "you succeeded to guess your Pincode in (one or two) trials"

package assign4;
import java.util.*;

public class assign4 {
    public static void main (String[]args) {
        Scanner sc = new Scanner(System.in);

        int min = 999;
        int max = 10000;
        int generatedPin = (int) (Math.random()*(max-min+1)+min);
        System.out.println("Please enter the pincode sent to you " + generatedPin);
        int input = sc.nextInt();

        if (input == generatedPin) {
          System.out.println("you succeeded to guess your pincode in one trial");
        }
        for(int i = 0; input != generatedPin && i<3; i++) {
          if (i==0) {
            System.out.println("wrong pin! two trial left!");
            input = sc.nextInt();
          } else

Why doesn't the following if condition run?

                if (i==1) {
                System.out.println("wrong pin! one trial left!");
                input = sc.nextInt();
                    if (input == generatedPin) {
                        System.out.println("you succeeded to guess your pincode in 1 trial");
                    }
                } 

else 
                    if (i==2)
                        System.out.println("wrong pin! we will lock your account");     
        }
}}

Aucun commentaire:

Enregistrer un commentaire