Omkar Omkar - 5 months ago 31
Java Question

Do While loop is not working for one block

Okay so pretty much I am making a bank machine project and I have been stumped with this one problem. You can deposit, withdraw and exit in this bank machine and everything is working fine except the withdraw part. Once I am done withdrawing however much money I would like it loops me back to the part where it asks me how much I would like to withdraw and therefore I am stuck in that endless loop. I am using a do while loop. I tried changing one of the while statements to if in the beginning of the withdraw code but that just terminates the program one I am done withdrawing.

Help would be appreciated thanks.
Also I did not post the full code so this does not include the public static void. I did this cause it would be to large.

int option;

do {

System.out.println("| Welcome Mr. " + name + " |"); // Fix
System.out.println("| Thank you for choosing Maze Bank |");
System.out.println(" Your current balance is $" + bal);
System.out.println("| Press [4] to deposit money |");
System.out.println("| Press [5] to withdraw money |");
System.out.println("| Press [6] to log off |");
option = input.nextInt();

if (option == 4) {
System.out.println("How much money would you like deposit?");
double deposit = input.nextInt();
System.out.println("You have deposited $" + deposit);
bal += deposit;
System.out.println("Your new balance is $" + bal);
System.out.println("Press [1] to return to the main menu");
option = input.nextInt();

if (option == 6) {
System.out.println("Logging out...");
// Exit message
System.out.println("Thank you for choosing Maze Bank!");
System.out.println("We hope to see you again!");
// Not working
while (option == 5 && bal <= 0) {
System.out.println("You don't have any money to withdraw");
System.out.println("Press [1] to return to the home screen");
option = input.nextInt();

while (option == 5 && bal > 0) {
System.out.println("How much money would you like to withdraw?");
double withdraw = input.nextInt();

while (withdraw > bal) {
System.out.println("Error, you cannot withdraw more than $" + bal);
withdraw = input.nextInt();
System.out.println("You have withdrawn $" + withdraw + ". Thank you.");
System.out.println("Press [1] to return to the main menu");
setting = input.nextInt();


} while (option == 1);





In your last while statement (option == 5 && bal > 0), you are assigning the wrong variable:

setting = input.nextInt()

Instead of:

option = input.nextInt()