while循环迭代

时间:2015-09-30 02:32:46

标签: java loops while-loop

我正在尝试创建一个while循环,其中用户总共有三次尝试输入有效数字。我不明白系统如何识别在显示消息之前已经进行了3次无效尝试。 创建类,变量和扫描程序对象。在三次尝试之后,我想说“不再尝试”。我已经编写了程序,如果有效,则使用用户输入的数量。这就是他们输入三次无效尝试。

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(); 

4 个答案:

答案 0 :(得分:0)

您似乎错过了循环的开始和结束括号。实际上,您的代码是

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

答案 1 :(得分:0)

编辑:在您更新问题时,所以有必要更新答案。这就是你真正想要的。

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

答案 2 :(得分:0)

dxdy关于制作while()循环函数所需的大括号是正确的。

一旦while循环结束(数量在1到20之间,或者尝试&gt; maxAttempts),你只需要有一个if语句,如下所示:

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

然后继续使用其余代码处理数量变量。

答案 3 :(得分:0)

试试这段代码:

        //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();
    }
}

虽然如果允许的话我可以把它变成方法