简体   繁体   English

如何继续要求用户选择有效选项?

[英]How to keep requesting user to select a valid option?

So, the user has to choose a number between 1 and 3. Otherwise, they're told to try again. 因此,用户必须在1到3之间选择一个数字。否则,将要求他们重试。 If the user tries a number less than 1 or greater than 3, whatever number they chose gets stored in the "choice" variable and causes the program to continue to run when it should just stop. 如果用户尝试使用小于1或大于3的数字,则他们选择的任何数字都会存储在“ choice”变量中,并使程序在应停止运行时继续运行。 I assumed there would be an easy solution, but apparently it's beyond me as a beginner. 我以为会有一个简单的解决方案,但是作为初学者,这显然超出了我的范围。 The obvious thing to me would be to somehow clear or empty the value that has been assigned to "choice" after the unsuccessful user input. 对我来说,显而易见的事情是,在用户输入失败后,以某种方式清除或清空已分配给“选择”的值。 Is that possible? 那可能吗?

import java.util.Scanner;

public class Furniture2Test  {

    public static void main(String[] args) {

        wood();

    } // end main

    public static void wood() {

        int choice;

        int pine = 1;
        int oak = 2;
        int mahogany = 3;

        int pineCost = 100;
        int oakCost = 225;
        int mahoganyCost = 310;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("What type of table would you like?");
        System.out.println("1. pine");
        System.out.println("2. oak");
        System.out.println("3. mahogany");

        choice = keyboard.nextInt();

        if (choice == 1) {
            choice = pineCost;
        } else if (choice == 2) {
            choice = oakCost;
        } else if (choice == 3) {
            choice = mahoganyCost;
        } else if (choice > 3 || choice < 1) {
            System.out.println("Try again.");
            choice = -1;
            wood();
        }

        System.out.println("That will be $" + choice + ".");

        size(choice);

    } // end wood

    public static void size(int choice) {

        int sizeChoice;
        int large = 35;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("What size will that be?");
        System.out.println("1. large");
        System.out.println("2. small");

        sizeChoice = keyboard.nextInt();

        if (sizeChoice == 1)
            System.out.println("That will be $" + (choice + large) + ".");
        else if (sizeChoice == 2)
            System.out.println("That will be $" + choice);
        else
            System.out.println("Please, enter either a 1 or a 2.");

    } // end size

}

Your requirement can be done easily with do...while loop. 使用do ... while循环可以轻松完成您的要求。 Sample code is as follows: 示例代码如下:

do{
    System.out.println("Choose option between 1 and 3");
    choice = keyboard.nextInt();
}while(!(choice > 3 || choice < 1));

if (choice == 1) {
    choice = pineCost;
} else if (choice == 2) {
    choice = oakCost;
} else if (choice == 3) {
    choice = mahoganyCost;
}

Hope this helps. 希望这可以帮助。

//put the menu logic
while(choice > 3 || choice < 1) {
    //put your try again logic.
}
//can only exit the while loop if the number is 1, 2, or 3, so put your output statement down here after the while loop
import java.util.Scanner;

public class Furniture2Test

{

   public static void main(String[] args)
   {

      wood();

   } // end main


   public static void wood()
   {

      int choice;

      int pine = 1;
      int oak = 2;
      int mahogany = 3;

      int pineCost = 100;
      int oakCost = 225;
      int mahoganyCost = 310;

      Scanner keyboard = new Scanner(System.in);

      System.out.println("What type of table would you like?");
      System.out.println("1. pine");
      System.out.println("2. oak");
      System.out.println("3. mahogany");

      choice = read_range(keyboard, 1, 3);

      if(choice == 1)
      {
         choice = pineCost;
      }
      else
         if(choice == 2)
         {
            choice = oakCost;
         }
         else
            if(choice == 3)
            {
               choice = mahoganyCost;
            }
            else
               if(choice > 3 || choice < 1)
               {
                  System.out.println("Try again.");
                  choice = -1;
                  wood();
               }

      System.out.println("That will be $" + choice + ".");

      size(choice);


   }  // end wood

   public static void size(int choice)
   {

      int sizeChoice;
      int large = 35; 

      Scanner keyboard = new Scanner(System.in);

      System.out.println("What size will that be?");
      System.out.println("1. large");
      System.out.println("2. small");

      sizeChoice = read_range(keyboard, 1, 2);

      if(sizeChoice == 1)
         System.out.println("That will be $" + (choice + large) + ".");
      else
         if(sizeChoice == 2)
            System.out.println("That will be $" + choice);
         else
            System.out.println("Please, enter either a 1 or a 2.");

   } // end size

   private static int read_range (Scanner scanner, int low, int high) {
     int value;
     value = scanner.nextInt();
     while (value < low || value > high) {
       System.out.print("Please enter a value between " + low + " and " + high + ": ");
       value = scanner.nextInt();
     }
     return value;
   }



} // end class

whatever number they chose gets stored in the "choice" variable and causes the program to continue to run when it should just stop// 他们选择的任何数字都存储在“ choice”变量中,并导致该程序在应该停止时继续运行//

the program is contining to run because you are calling wood() if(choice > 3 || choice < 1) 该程序正在继续运行,因为您正在调用wood()if(choice> 3 || choice <1)

if you want it to stop remove the wood() call 如果您希望它停止,则删除wood()调用

if you also want to clear the value for choice(instead of -1) you can assign it to null 如果您还想清除选择值(而不是-1),则可以将其分配为null

choice is a local variable to the method wood, you are making a recursive call to wood when the user makes a wrong choice. choice是wood方法的局部变量,当用户做出错误选择时,您将递归调用wood This is an interesting design choice and probably not the best in this case. 这是一个有趣的设计选择,在这种情况下可能不是最佳选择。

When you call wood again, choice is rest (in this to unknown value until it is assigned value from the user). 当您再次打电话给wood时,选择权就落在了这里(在此之前,它是未知值,直到它被用户分配了值)。

Now the problem occurs when the wood method exists...each time it returns to the caller, it will call size(choice) , where choice is -1 (because that's what you set it to before calling wood again). 现在问题出现了,当存在wood方法时...每次返回到调用方时,它将调用size(choice) ,其中choice-1 (因为这是您在再次调用wood之前将其设置为的值)。

  1. You should be using a while-loop instead of recursive calls 您应该使用while-loop而不是递归调用
  2. You should never call size(choice) with anything other then a valid choice 除有效选择外,切勿使用其他任何东西呼叫size(choice)

Take a look at The while and do-while statement for more details 看一下while和do-while语句以了解更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何使用while循环来不断请求用户输入 - How would I use a while loop to keep requesting user input 如果用户输入无效选项,则请求相同的输入 - Requesting the same input if user enters an invalid option 此选项如何有效? -classpath混乱 - How this option is valid? -classpath confusion 如何继续循环直到用户输入有效长度的字符串? - How to keep looping through until user has inputted string of valid length? 我如何不断询问用户有效的整数输入,如果输入有多个约束 - How do I keep asking a user for a valid integer input if there is more than one constraint to the input 除了请求的用户之外,如何检查电子邮件是否存在? - How to check if email exists, except the user who is requesting? 如何使为用户提供的下载网址有效一分钟,时间一过去,Web应用程序应重定向到错误页面 - How to keep an download url given for a user valid for a minute , once the time is elapsed, the web app should redirect to error page 如果发生错误,如何提示用户重新输入值,以及如何提供继续检查工资率或退出程序的选项? - How to prompt the user to re-enter a value if a mistake was made and How to give an option to keep on checking pay rates or to exit the program.? 如何在DropDownChoice Wicket中保留“选择一个”选项? - How to keep “Choose One” option in a DropDownChoice Wicket? 如何将默认选项更改为用户定义的选项之一,例如 select 国家/地区? - How to change the default option choose one to user defined one like select country?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM