u/Few-Future-6714

Help me with this logic and why it failed im still learning

So i wrote this:

 public int createAccNumber(){

    int n;
    Random r = new Random();
    n= r.nextInt(50);

    accN = n;    
 
if(n == accN)
{        
 createAccNumber();    
 }
    

     return accN; 
}

and the correct way

public int createAccNumber(){
        int n;
        Random r = new Random();
        n= r.nextInt(50);
        boolean isTaken = false;

        for(Account a : BankManager.allAccounts){
            if(a.getAccNum() == n){
                isTaken = true;
                break;
            }
            if(isTaken){return createAccNumber();}

            return n;
        }
           accN = n;
        if(n == accN){
            createAccNumber();
        }

        return accN;
    }

Sorry for using that way its my first time writing code like this

reddit.com
u/Few-Future-6714 — 5 days ago