Confused with recursive methods and loops

I wrote the method below, but it doesn't work correctly.

Even if the output is incorrect, this program executes the following method in the main class.

The basic idea is that your output is correct and the method ends and the program moves on to the next method. If the PIN is wrong then you will have 3 times. If all efforts were wrong than the program would come out. Thus, your card will be blocked. Please give me some advice.

public boolean authenticity(short pin)  {
       if (pin == 1234) {
           System.out.println("PIN is correct");
           System.out.println("Card is active for operation!");
           return true;
       } else {
           pin = sc.nextShort();
           for (int i = 1; i >= 3; i++) {
               System.out.println("PIN isn't correct! You have " +i +"effort(s)");
               return authenticity(pin);  // recursion
           }
       }
       return false;
  }

      

* In the main class, the method is executed according to the command: Authenticity (sc.nextShort ());

+3


source to share


5 answers


Your method doesn't make a lot of sense ...

Since you are trying to recursively input output until the user makes correct input ... then authenticity

will always return true

or continue indefinitely

This is how the code works ... You need to call the method authenticity

passing the number of retries before returning false

.



If you have no more attempts, il will return false

, otherwise you will get a new contact and check the correctness ... If the number is incorrect, print recursively authenticity

, decreasing the number of repetitions by one ...

authenticity(3); // call the method passing 3 as max number of tries...

public static boolean authenticity(int tries) {
          if (tries > 0)
          {
           short pin = sc.nextShort();
           if (pin == 1234) {
               System.out.println("PIN is correct");
               System.out.println("Card is active for operation!");
               return true;
           } else {
               System.out.println("PIN isn't correct! You have " + tries +"effort(s)");
               return authenticity(--tries); 
           }
          } 
          return false;
      }

      

I deleted for

it because it didn't make much sense.

+2


source


First of all, the loop condition must be i> 0:

       for (int i = 3; i > 0; i--) {
           System.out.println("PIN isn't correct! You have " +i +"effort(s)");
           return authenticity(pin);  
       }

      

Second, in your current implementation, each recursive call will give the user 3 more tries (at least until it happens StackOverflow

). You must pass the number of attempts remaining as a parameter to the recursive call. And you don't need a loop.



public boolean authenticity(short pin, int remainingAttempts) {
    if (pin == 1234) {
        System.out.println("PIN is correct");
        System.out.println("Card is active for operation!");
        return true;
    } else {
        pin = sc.nextShort();
        remainingAttempts--;
        if (remainingAttempts > 0) {
            System.out.println("PIN isn't correct! You have " +remainingAttempts +" attempts left");
            return authenticity(pin,remainingAttempts);
        }
    }
    return false;
}

      

If you want to keep the loop, you can get rid of the recursion.

+5


source


public boolean authenticity(short pin, int tried) {
    if (pin == 1234) {
        System.out.println("PIN is correct");
        System.out.println("Card is active for operation!");
        return true;
    } else {
        pin = sc.nextShort();
        System.out.println("PIN isn't correct! You have " +tried +"effort(s)");
        if(tried==0)
            return false;
        return authenticity(pin,--tried);
    }
    return false;
}

      

use tried == 3

; if you want more use more

+1


source


It looks like you are missing some key steps for recursion. In this case, it would probably be better to use simple iteration rather than recursion, and arrange the code a little better. It was not entirely clear if your scanner was and such were local variables in another method or fields, and your recursion was wrong as it never passed i

(field) when calling itself.

It seems that you are doing some scanner readings of the same thing in another part of the code, which should be happening in the same place for organizational reasons:

public boolean authenticateUser(Scanner sc) {
   for(int tries = 0; tries<3; tries++){
       short attempt = sc.nextShort();
       if(attempt==1234) return true;
   }
   return false;
}

      

If you still want to use recursion:

public boolean authenticateUser(short pin, int triesLeft) {
   if(triesLeft==0){
       System.out.println("You are out of tries.");
       return false;
   }
   if (pin == 1234) {
       System.out.println("PIN is correct");
       System.out.println("Card is active for operation!");
       return true;
   } else {
       pin = sc.nextShort();
       System.out.println("PIN isn't correct! You have " +i +"effort(s)");
       return authenticity(pin, triesLeft-1);  //it recurcy
       }
   }
}

      

0


source


Run how the code works. Let's say you fixed the loop.

authenticity(1):

public boolean authenticity(short pin) `{`
       if (pin == 1234) { // 1 == 1234 == FALSE
           System.out.println("PIN is correct");
           System.out.println("Card is active for operation!");
           return true;
       } else { // PASS
           pin = sc.nextShort(); // USER GETS TO TYPE SOMETHING NEW. Lets say "2"
           for (int i = 3; i > 0; i--) { // EXECUTES THE FOLLOWING THREE TIMES
           System.out.println("PIN isn't correct! You have " +i +"effort(s)"); // What does this print? Why?
           return authenticity(pin);  // EXCEPT RETURNS ON THE FIRST LOOP
           }
       }
       return false;
  }


authenticity(1):
  authenticity(2): 

public boolean authenticity(short pin) `{`
       if (pin == 1234) { // 2 == 1234 == FALSE
           System.out.println("PIN is correct");
           System.out.println("Card is active for operation!");
           return true;
       } else { // PASS
           pin = sc.nextShort(); // USER GETS TO TYPE SOMETHING NEW. Lets say "3"
           for (int i = 3; i > 0; i--) { // EXECUTES THE FOLLOWING THREE TIMES
           System.out.println("PIN isn't correct! You have " +i +"effort(s)"); // What does this print? Why?
           return authenticity(pin);  // EXCEPT RETURNS ON THE FIRST LOOP
           }
       }
       return false;
  }

authenticity(1):
  authenticity(2): 
    authenticity(3): 

public boolean authenticity(short pin) `{`
       if (pin == 1234) { // 3 == 1234 == FALSE
           System.out.println("PIN is correct");
           System.out.println("Card is active for operation!");
           return true;
       } else { // PASS
           pin = sc.nextShort(); // USER GETS TO TYPE SOMETHING NEW. Lets say "4"
           for (int i = 3; i > 0; i--) { // EXECUTES THE FOLLOWING THREE TIMES
           System.out.println("PIN isn't correct! You have " +i +"effort(s)"); // What does this print? Why?
           return authenticity(pin);  // EXCEPT RETURNS ON THE FIRST LOOP
           }
       }
       return false;
  }

authenticity(1):
  authenticity(2): 
    authenticity(3): 
       authenticity(4): 

public boolean authenticity(short pin) `{`
       if (pin == 1234) { // 4 == 1234 == FALSE
           System.out.println("PIN is correct");
           System.out.println("Card is active for operation!");
           return true;
       } else { // PASS
           pin = sc.nextShort(); // USER GETS TO TYPE SOMETHING NEW. Lets say "5"
           for (int i = 3; i > 0; i--) { // EXECUTES THE FOLLOWING THREE TIMES
           System.out.println("PIN isn't correct! You have " +i +"effort(s)"); // What does this print? Why?
           return authenticity(pin);  // EXCEPT RETURNS ON THE FIRST LOOP
           }
       }
       return false;
  }

      

0


source







All Articles