JacobMauk
JacobMauk

Reputation: 3

Java input stops working when Do/While is fulfilled

I am trying to create a running loop where the user can input oysters entering/leaving the oyster bar until the capacity is fulfilled at 500.

When the capacity hits 500 I would like the user to still be able to input negative numbers (because patrons can still leave) but not input any positive numbers. Unfortunately, I just cant input anything once that condition is fulfilled.

    Scanner input = new Scanner(System.in);
        int maxCapacity = 500;
        int oystersIn = 0;
        int oystersOut;
        int currentCapacity = 0;
    System.out.println("Welcome to the Half Shell Dive Bar, Capacity is 500, and there are currently "+ currentCapacity + " oysters in the bar" );
    currentCapacity = 0;
    do {
        oystersIn=0;
        System.out.println("How many oysters are coming in?");
        oystersIn = input.nextInt();        
        currentCapacity += oystersIn;
        System.out.println("Now there are " + currentCapacity + " oysters in the bar");
    }
    while (oystersIn + currentCapacity <= 500);

    }

}

any input would be greatly appreciated, thank you!

Upvotes: 0

Views: 49

Answers (2)

Nayan
Nayan

Reputation: 5

I would suggest you to:-
1)run another do while loop which takes negative input. 2)Put both of the loop in a infinite loop if you want to re run program if oysters are no longer is available and use "break;" to come out from infinite loop. hope this helps

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

Assuming you want to loop forever, you need to check for under and over capacity

do {
    oystersIn=0;
    System.out.println("How many oysters are coming in?");
    oystersIn = input.nextInt(); 
    if (oystersIn + currentCapacity >= 500) {
        System.out.println("Sorry full - waiting for some one to leave");
        continue;
    }
    if (oystersIn + currentCapacity < 0) {
        System.out.println("Can't go negative");
        continue;
    }
    currentCapacity += oystersIn;
    System.out.println("Now there are " + currentCapacity + " oysters in the bar");
}
while (true);

Upvotes: 1

Related Questions