[英]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之前将其设置为的值)。
while-loop
instead of recursive calls 您应该使用while-loop
而不是递归调用 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.