簡體   English   中英

While循環與迭代

[英]While loop with iteration

我正在嘗試創建一個while循環,在該循環中,用戶總共嘗試輸入三個有效數字。 我不了解系統在顯示消息之前如何識別出已經進行了3次無效嘗試。 類,變量和掃描器對象已制成。 經過三次嘗試,我想說“不再嘗試”。 我已經編寫了程序來使用用戶輸入的數量(如果有效)。 就像他們輸入了三個無效嘗試一樣。

Updated code: 

int數量= 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(); 

您似乎缺少該循環的左括號和右括號。 照原樣,您的代碼讀取

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++;

相反,你應該做

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

編輯:當您更新問題時,所以也有必要更新答案。 這就是您真正想要的。

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;
        }
    }
}

對於使while()循環函數所需的花括號, dxdy是正確的。

一旦while循環結束(數量在1到20之間,或者嘗試> maxAttempts),您只需要有一個if語句,如下所示:

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

雖然如果允許的話,我本可以將其轉換為方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM