简体   繁体   中英

While loop with iteration

I am trying to create a while loop where the user has a total of three tries to enter a valid number. I'm not understanding how the system recognizes that 3 invalid attempts have been made before displaying the message. Classes, variables, and scanner objects are made. After the three attempts, I want to say "No more tries". I already have the program written to use the user's input for quantity if its valid. This is just if they input three invalid attempts.

Updated code: 

int quantity = 0;

        // Get user's desired amount of lemonade cups
        System.out.print("Hello " + name + ". How many cups of lemonade can I get you? ");
        quantity = keyboard.nextInt(); // Store amount of cups wanted

        int attempts = 0;
        int maxAttempts = 3;
        double subTotal = quantity * lemonadeCost;
        double totalTax = subTotal * 0.08;
        double totalPrice = subTotal + totalTax;

        while (attempts < maxAttempts) {
            if (quantity < 1 || quantity >= 20) {
                System.out.println("That is an invalid amount, please try again");
                quantity = keyboard.nextInt(); }

             else {
                 System.out.println("Subtotal: " + defaultFormat.format(subTotal));
                 System.out.println("Tax: " + defaultFormat.format(totalTax));
                 System.out.println("Total: " + defaultFormat.format(totalPrice)); 

            }
             attempts++;
                if (attempts >= 3) {
                    System.out.print ("No lemonade for you");
                    break;
                }
            // Ask for user's payment method
            Scanner method = new Scanner(System.in);
            System.out.println("How would you like to pay? Enter 'm'   for money, 'c' for credit or 'g' for gold. ");
            String payment = method.nextLine(); 

You seem to be missing the opening and closing brackets for the loop. As it is, your code reads

while (quantity < 1 || quantity >= 20 && attempts <= maxAttempts)
   System.out.println("That is an invalid amount, please try again"); 
// these below are not part of the loop
quantity = keyboard.nextInt();
attempts++;

Instead you should do

while (quantity < 1 || quantity >= 20 && attempts <= maxAttempts){
   System.out.println("That is an invalid amount, please try again"); 
   quantity = keyboard.nextInt();
   attempts++;
}

EDIT: As you updated the question, so it was necessary to update the answer too. Here it is what you actually want.

public static void main(String[] args) {
    // Get user's desired amount of lemonade cups
    String name = "Jimmy Nguyen";
    Scanner keyboard = new Scanner(System.in);
    int quantity;// Store amount of cups wanted
    int lemonadeCost = 4; // Suppose
    int attempts = 0;
    int maxAttempts = 3;
    System.out.print("Hello " + name + ". How many cups of lemonade can I get you? ");
    while (attempts < maxAttempts) {

        quantity = keyboard.nextInt();
        if (quantity < 1 || quantity >= 20) {
            System.out.println("That is an invalid amount, please try again\n");
            ++attempts;
        } else {
            double subTotal = quantity * lemonadeCost;
            double totalTax = subTotal * 0.08;
            double totalPrice = subTotal + totalTax;
            System.out.println("Subtotal: " + subTotal);
            System.out.println("Tax: " + totalTax);
            System.out.println("Total: " + totalPrice);
            // Ask for user's payment method
            System.out.println("How would you like to pay? Enter 'm'   for money, 'c' for credit or 'g' for gold. ");
            keyboard.nextLine();
            String payment = keyboard.nextLine();
            break;
        }

        if (attempts >= 3) {
            System.out.print("No lemonade for you");
            break;
        }
    }
}

dxdy is correct about the braces required for making the while() loop function.

Once the while loop has ended (either quantity is between 1 and 20, or attempts > maxAttempts), you just need to have an if statement like the following:

if (attempts > maxAttempts) {
  System.out.println("No more tries);
  return -1; // or something else to break out of your code
}

and then continue on with the rest of your code working with the quantity variable.

try this code:

        //start with 1 since the user will attempt it at least one time.
        int attempts = 1;

        int maxAttempts = 3;

        int quantity=0;

        Scanner keyboard = new Scanner(System.in);

        //LESS THAN OR EQUAL TO 3... 
        while(attempts<=maxAttempts){

            System.out.print("Enter amount: ");

            quantity = keyboard.nextInt();

            //check if valid

            if(quantity < 1 || quantity >= 20){


                //check if it's 1st and 2nd trial.
                if(attempts<maxAttempts){

                    System.out.println("That is an invalid amount, please try again");

                }else{
                    //third trial and still invalid
                    System.out.print("No more tries");

                }

            }else{

                //user entered a valid amount so let's break the loops. 
                System.out.println("The amount is valid. Value of amount: "+ quantity);
                break;
            }
            //increment attempts
            attempts++;
        }

        //never forget to close the scanner
        keyboard.close();
    }
}

Though I could have turned this into methods if it's allowed

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