Home > Blockchain >  Do while loop misfunction
Do while loop misfunction

Time:04-14

This is my code:

public boolean displayLoginScreen()
    {
        int choice;
        boolean isStaff = true;
        Scanner input  = new Scanner(System.in);
        System.out.print("****************************************************\n");
        System.out.print("                                                    \n");
        System.out.print("         Grocery Store Management System            \n");
        System.out.print("                                                    \n");
        System.out.print("****************************************************\n");
        System.out.println();
        System.out.print("             Do you want to log in as : \n");
        System.out.print("                   1 = Staff \n");
        System.out.print("                   2 = Customer\n");
        System.out.print("                   3 = Exit\n");
        System.out.println();
        
        try
        {
            do 
            {
                System.out.print("Enter your choice : "); 
                choice = input.nextInt();
                System.out.println(); 
                 
                switch(choice)
                {
                    case 1:
                    {
                        isStaff = staffLogin();
                        
                        if(isStaff)
                            return isStaff;
                        else
                            displayLoginScreen();
                        
                        break;
                    }
                    
                    case 2:
                    {
                        customerLogin();
                        return isStaff = false;
                    }
                    
                    case 3:
                        System.exit(0);
                        break;
                        
                    default:
                        System.out.println("Invalid input. Enter again.");
                        System.out.println();
                }
                
            } while(choice != 3);
        }
            
        catch(InputMismatchException e)
        {
            System.out.println("Invalid input. Enter again.");
            System.out.println();
            displayLoginScreen();
        }
        
        return isStaff;
    }

When i run for the first time and enter 1 for choice and the isStaff is false. And then the displayLoginScreen() will run. But for this second time, my choice == 1 and isStaff is true, however, the return isStaff in case 1 did not function and the program run again the do while loop where it ask me to enter my choice again.

CodePudding user response:

Because when you recursively call displayLoginScreen() (which you probably shouldn't be doing in the first place, recursion is not a replacement for just repeating logic in a loop), the code never does anything with the result. To put it simply, the if block here returns a value, the else block does not:

if (isStaff)
    return isStaff;
else
    displayLoginScreen();

If the intent is to return the result of displayLoginScreen(), return it:

if (isStaff)
    return isStaff;
else
    return displayLoginScreen();
  • Related