简体   繁体   中英

Can't get Java do while loop to break

A simple yet disgruntling problem with my wallet program, everything seems to be working properly when adding money to my wallet but I can't seem to break out of my do-while loop when inputing "-1" as my moneyChoice.

private static void AddItemToWallet()throws IOException{
boolean valid=true;
String input;
int moneyChoice;
char addToWallet;

do{
  out.println("\n\n======================================================");
  do{
    out.println("Please enter choice to add (integer) --->");
    //display money choices
    System.out.println("Avaliable moneys to add:\n");
    for (Money money : Money.values()) {
      System.out.print(money.ordinal()+1 + " --- ");
      System.out.print(money + ": ");
      System.out.println(money.GetDenom());
    }
    //scanner=new Scanner(in);
    moneyChoice=scanner.nextInt();
    if (moneyChoice > 11 || moneyChoice <= -2 || moneyChoice == 0) {
      out.println("Your choice is not valid.");
    } 
  }while(moneyChoice > 11 || moneyChoice <= -2 || moneyChoice ==0);

  out.println(moneyChoice);
  do{
    try{
      valid=false;
      scanner=new Scanner(in);
      //confirm choice
      out.println("Add item to wallet? (y/n)");
      input=scanner.nextLine();
      addToWallet=input.charAt(0);
      if (Character.toLowerCase(addToWallet)=='n'){
        out.println("Selection " + moneyChoice + " was not added to wallet.");
        valid=true;
      }else if (Character.toLowerCase(addToWallet)=='y'){
        out.println("Selection " + moneyChoice + " was added to wallet.");
        valid=true;

        Money money = Money.PENNY; 
        if(moneyChoice ==1){ 
          money = Money.PENNY;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==2){ 
          money = Money.NICKLE;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==3){ 
          money = Money.DIME;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==4){ 
          money = Money.QUARTER;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==5){ 
          money = Money.HALFDOLLAR;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==6){ 
          money = Money.ONE;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==7){ 
          money = Money.FIVE;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==8){ 
          money = Money.TEN;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==9){ 
          money = Money.TWENTY;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==10){ 
          money = Money.FIFTY;
          wallet.add(money.GetDenom());
        }
        if(moneyChoice ==11){ 
          money = Money.HUNDRED;
          wallet.add(money.GetDenom());
        }

        System.out.println(money);



        //Using a Switch and set ENUM
        Money foundMoney = money;
        switch (foundMoney) {
          case PENNY:
            money = Money.PENNY; 
            System.out.println("Pennies are useless");
            break;
          case NICKLE:
            System.out.println("Why did the fan go to the concert? To get their Nickle back!!! Man are they horrible.");
            break;
          case DIME:
            System.out.println("How come Dimes are the smallest coin? So strange.");
            break;
          case QUARTER:
            System.out.println("Tom Brady, best Quarterback eva!!!!");
            break;
          case HALFDOLLAR:
            System.out.println("Half Dollar coin. Have you seen one???");
            break;
          case ONE:
            System.out.println("One is the lonliest number.");
            break;
          case FIVE:
            System.out.println("Five Buck Lunch, here I come!!!");
            break;
          case TEN:
            System.out.println("Ten is what grandma gave you for graduation.");
            break;
          case TWENTY:
            System.out.println("Twenty Bucks. 2 0... awesome.");
            break;
          case FIFTY:
            System.out.println("Fitty Sploondocks.");
            break;
          case HUNDRED:
            System.out.println("I did it for the Benjamins. Get rich or die trying.");
            break;
        }
      }
    }catch(Exception ex){

    }
    if (!valid){
      out.println("That is not a valid answer");
    }
  }while(!valid);  
 }while(moneyChoice!= -1);

}

Your program can basically be reduced to the following

do {
    // read moneyChoice

    do {

         // Perform logic

    } while ( ! valid );

} while ( moneyChoice != -1 );

Note that after you read moneyChoice , you always go into the do that performs the logic. There is nothing there that checks whether the moneyChoice you entered was -1 . So the logic is performed anyway, and only then, when you hit while ( moneyChoice != -1 ) , it will stop.

You should add an if such that the logic will only be performed if the moneyChoice that was read is not -1.

do {
    // read moneyChoice

    if ( moneyChoice != -1 ) {
        do {

         // Perform logic

        } while ( ! valid );
    }

} while ( moneyChoice != -1 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM