Pergunta

Problem specification: Infinite loop ( menu enters an infinite loop );

Objective: Request user for an integer input and proceed in accordance to specified logic inside of the code. How to avoid the infinite loop?;

Code:

    public Purchase groceryStoreMenu(LemonadeStand lemonadeStand){

    boolean getMenu = true;
    int userEnteredNumber = -1;
    currentPurchase = new Purchase();

    while(getMenu){
         try{

           System.out.println("Grocery Store");
           System.out.printf("%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n" , "1:" , "Buy lemons", "2:", "Buy cups" , "3:" , "Buy sugar" , 
           "4:" , "Buy ice" , "5:" , "Done"); //change this 
           userEnteredNumber = reader.nextInt();

           if (userEnteredNumber == 1 ) {
              money = lemonadeStand.profit(0);
              lemonsMenu(money);
           }else if (userEnteredNumber == 2){
              money = lemonadeStand.profit(0);
              cupsMenu(money); 
           }else if (userEnteredNumber == 3){
              money = lemonadeStand.profit(0);
              sugarMenu(money); 
           }else if (userEnteredNumber == 4){
               money = lemonadeStand.profit(0);
               iceMenu(money); 

           }else{
              money = lemonadeStand.profit(0);
             dailyGreetingMenu(); 

           }

        if (userEnteredNumber != 1 && userEnteredNumber !=2 && userEnteredNumber != 3 
            && userEnteredNumber != 4 && userEnteredNumber != 5) {
     throw new Exception(); 
    } else if(userEnteredNumber == 6) {
        getMenu = false;
        //break;
    } else {
        getMenu = false;
        //break;
    }

    }

    catch(Exception e) 
    {
        System.out.println("Error in number format. Enter a valid number from the choices (1,2,3,4,5,6)");
    }

    }


    return currentPurchase;
Foi útil?

Solução

Your program never reaches if(userEnteredNumber == 6) with 6 value, because if userEnteredNumber is 6, the first condition will grab it:

if (userEnteredNumber != 1 && userEnteredNumber !=2 && userEnteredNumber != 3 
        && userEnteredNumber != 4 && userEnteredNumber != 5) {
    throw new Exception(); 
}

then it throws exception, and it is catched within loop block. And any number other than 1, 2, 3, 4, 5, 6 is the same. So the loop never ends.

I don't like your code-style at all. But if you want to do it this way, you shall add userEnteredNumber != 6 to first if condition.

Outras dicas

looks like the execution is never gonna enter the following block: else if(userEnteredNumber == 6) { getMenu = false; //break; }

the if condition should not contain '&&'.it should have been '||' Also looks like you dont want your user to enter any number other than 1,2,3,4,5.

if (userEnteredNumber != 1 || userEnteredNumber !=2 || userEnteredNumber != 3 
        || userEnteredNumber != 4 || userEnteredNumber != 5) {
    getMenu = false;               
    throw new Exception();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top