user4154395
user4154395

Reputation:

How do I get out of this freaking loop

I understand that while the state is true the loop will keep running. I thought if I simply typed in loop=false, after the } bracket for the loop I could continue to code. Obviously I was wrong, it wont run anything. SOMEONE please show me how to get out of this hell of a while loop.

System.out.println("You total balance is 0.00, "
                + "please deposit coins and type done when finished");

        while(loop){
            if (input.hasNextInt()){
                deposit = input.nextBigDecimal();}
                String change = input.next();

        switch (change){

        case "quarter":
            balance= quarter.multiply(deposit);
            total=total.add(balance);
            System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
            break;

        case "dime": 
            balance=dime.multiply(deposit);
            total=total.add(balance);
            System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
            break;
        case "nickel":
            balance=nickel.multiply(deposit);
            total=total.add(balance);
            System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
            break;
        case "penny":
            balance=penny.multiply(deposit);
            total=total.add(balance);
            System.out.println("Your balance is "+ total +" :Make addiontal deposit(s)");
            break;
        case"done":
            System.out.println("Your total is $"+total);
            fee=total.multiply(feeRate);
            System.out.println("The exchance fee is 9.1% which amounts to $"+fee);
            grandTotal=total.subtract(fee);
            System.out.println("Your total balance minus the exchange fee is $"+grandTotal);

            break;

        default: System.out.println("There is a issue at "+change);}

            } System.out.println("4");

        }

}

Upvotes: 2

Views: 102

Answers (4)

chillichief
chillichief

Reputation: 1212

You have to change the value of the variable loop inside oft the while loop to false.

I guess this should be in the case when the user types done. So in this case add the following line:

loop = false;

anywhere in the case "done": block

Upvotes: 1

ThePerson
ThePerson

Reputation: 3236

Your loop will continue to go round until the loop variable is set to false. In the "done" case, just set that to false. That way, it won't loop back around again.

        case"done":
            System.out.println("Your total is $"+total);
            fee=total.multiply(feeRate);
            System.out.println("The exchance fee is 9.1% which amounts to $"+fee);
            grandTotal=total.subtract(fee);
            System.out.println("Your total balance minus the exchange fee is $"+grandTotal);
            loop = false; // This is the line to add.
            break;

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

The key is to change the loop variable so that it becomes false and will thus have you exit the while loop. So change the loop variable in the case "done" block. In that block, set loop = false;

    case"done":
        System.out.println("Your total is $"+total);
        fee=total.multiply(feeRate);
        System.out.println("The exchance fee is 9.1% which amounts to $"+fee);
        grandTotal=total.subtract(fee);
        System.out.println("Your total balance minus the exchange fee is $"+grandTotal);

        loop = false;   // ************  add this ************

        break;

Keep in mind that the key concept is that the thing that is causing the loop to continue must be changed somehow within the loop itself. Otherwise the while loop will never end.

Upvotes: 5

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

Three methods to break a loop.

A) set your flag variable to false:

loop = false;

Since you already have this variable, this is the much nicer (and simpler) approach.

B) use a labeled break.

label: while(true) {
  // ...
  switch(something) {
    break label; // Without a label, it would only leave the switch!
  }
}

Some people will frown upon this last solution, because it is essentially a "goto".

C) use more modular code.

In many cases, the much cleaner way is to use methods. Then you can have "break" to exit an inner loop or switch and return to actually leave the subprocedure and return a result.

This leads to easier-to-understand code, and the semantics of a return are often much more appropriate than those of a break.

Upvotes: 3

Related Questions