繁体   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