Java 如何不断要求用户选择有效选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19016090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep requesting user to select a valid option?
提问by user2817128
So, the user has to choose a number between 1 and 3. Otherwise, they're told to try again. 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. 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?
因此,用户必须在 1 和 3 之间选择一个数字。否则,他们会被告知再试一次。如果用户尝试一个小于 1 或大于 3 的数字,他们选择的任何数字都会存储在“选择”变量中,并导致程序在应该停止时继续运行。我认为会有一个简单的解决方案,但显然它超出了我作为初学者的能力。对我来说显而易见的事情是以某种方式清除或清空在用户输入失败后分配给“选择”的值。那可能吗?
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
}
回答by nhgrif
//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
回答by necromancer
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
回答by Kaushik Sivakumar
whatever number they chose gets stored in the "choice" variable and causes the program to continue to run when it should just stop//
他们选择的任何数字都会存储在“选择”变量中,并导致程序在应该停止时继续运行//
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
回答by MadProgrammer
choice
is a local variable to the method wood, you are making a recursive call to wood
when the user makes a wrong choice. This is an interesting design choice and probably not the best in this case.
choice
是方法 wood 的局部变量,wood
当用户做出错误选择时,您将进行递归调用。这是一个有趣的设计选择,在这种情况下可能不是最好的选择。
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)
, where choice
is -1
(因为这是你在再次调用 wood 之前设置的)。
- You should be using a
while-loop
instead of recursive calls - You should never call
size(choice)
with anything other then a valid choice
- 您应该使用 a
while-loop
而不是递归调用 size(choice)
除了有效的选择之外,你永远不应该用任何东西打电话
Take a look at The while and do-while statementfor more details
查看while 和 do-while 语句以获取更多详细信息
回答by SachinSarawgi
Your requirement can be done easily with do...while loop. Sample code is as follows:
使用 do...while 循环可以轻松完成您的要求。示例代码如下:
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.
希望这可以帮助。