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

Time:11-14

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");     
        }
}}

CodePudding user response:

as in your loop condition you have set, the loop will immediately break if input == generatedPin and therefore 'if' becomes unreachable code

CodePudding user response:

Assuming I understood the requirements correctly the following code will do what you need:

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");
        } else {
            System.out.println("wrong pin! two trial left!");
            for (int i = 0; i < 2; i  ) {
                if (i == 0) {
                    input = sc.nextInt();
                    if (input == generatedPin) {
                        System.out.println("you succeeded to guess your pincode in two trials");
                        break;
                    } else {
                        System.out.println("wrong pin! one trial left!");
                    }
                } else {
                    input = sc.nextInt();
                    if (input == generatedPin) {
                        System.out.println("you succeeded to guess your pincode in three trials");
                    } else {
                        System.out.println("wrong pin! we will lock your account");
                    }
                }
            }
        }
    }
}

As in my comment, your code is hard to read and I was not. even able to make it compile.

Basically what you need is a for loop that runs two times so that the user has a total of 3 trials to guess the pincode. If in the first one he guesses it you need to exist the for loop. If he fails again, the for loop will run one last time.

  • Related