#While statement exit

1 messages · Page 1 of 1 (latest)

steady geyser
#

Main class

            do {
            if (CAmount >= 1 && CAmount <= 20) { // Logic to check that user can't buy X amount out of bounds
                System.out.println("Order of " + CAmount + " lottery tickets confirmed");
                buy1.tickets(CAmount);
                buy1.CTimes = 1;
            } else {
                System.out.println("Order invalid you can only buy between 1 and 20 tickets at the moment");
                System.out.println("How many lotter tickets would you like: ");
                String TAmount = br.readLine();
                int TCAmount = Integer.parseInt(TAmount);
                CAmount = TCAmount;
            }
            } while (CAmount < 1 && CAmount >= 21 || buy1.CTimes < 1);

Lottery class

    int CTimes = 0; // Int used to make sure that while loop doesn't repeat infinitly for ticket purchase in main
    
    void tickets(int Amount) { // Amount of tickets to be bought
            
        if (Amount <= 5 && Amount >= 1) {
            System.out.println("Confirmed you have purchased " + Amount + " lottery tickets");
            chance = 0.25;
            AWon = 5;
            CTimes++;
        } else if (Amount <= 10 && Amount >= 6) {
            System.out.println("Confirmed you have purchased " + Amount  + " lottery tickets");
            chance = 0.50;
            AWon = 10;
            CTimes++;
        } else if (Amount <= 15 && Amount >= 11) {
            System.out.println("Confirmed you have purchased " + Amount + " lottery tickets");
            chance = 0.75;
            AWon = 15;
            CTimes++;
        } else {
            System.out.println("Confirmed you have purchased " + Amount + " lottery tickets");
            chance = 1.00;
            AWon = 20;
            CTimes++;
        }
weak hemlockBOT
steady geyser
#

Having this issue, where in the main class the user inputs say 12 which then calls the lottery class and runs that. However, once done it doesn't exit the while loop in the main, I'm a bit confused as to why. The int CTimes, is incremented by one in the lottery class as the if statement is run, it is then checked in the main while statement and should exit that while loop as 1 is not < 1

weak hemlockBOT
#

<@&987246399047479336> please have a look, thanks.

weak hemlockBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

The code you provided is incomplete and contains a syntax error. The System.out.printl statement is not complete and should be followed by a closing parenthesis. Additionally, there is no closing bracket for the do statement.

To exit the Main class using a while statement, you can use a boolean variable as a condition for the loop. Here's an example:

public class Main {
    public static void main(String[] args) {
        boolean exit = false; // Variable to control the loop
        
        do {
            if (CAmount >= 1 && CAmount <= 20) {
                System.out.println("Valid amount");
                // Other logic or actions
            } else {
                System.out.println("Invalid amount");
                exit = true; // Set exit to true to break out of the loop
            }
        } while (!exit);
        
        // Code outside the loop
    }
}

In this example, if CAmount is between 1 and 20 (inclusive), it will execute the logic for a valid amount. If it's not within that range, it will print "Invalid amount" and set exit to true, causing the loop to exit. You can place any additional code outside of the loop after it.