Java 如何继续请求用户选择有效选项?
因此,用户必须在1和3之间选择一个数字。否则,他们会被告知再试一次。如果用户尝试的数字小于1或大于3,则他们选择的任何数字都会存储在“choice”变量中,并使程序在应该停止时继续运行。我以为会有一个简单的解决方案,但作为一个初学者,我显然无法理解。对我来说,最明显的事情是在用户输入失败后,以某种方式清除或清空分配给“choice”的值。可能吗Java 如何继续请求用户选择有效选项?,java,Java,因此,用户必须在1和3之间选择一个数字。否则,他们会被告知再试一次。如果用户尝试的数字小于1或大于3,则他们选择的任何数字都会存储在“choice”变量中,并使程序在应该停止时继续运行。我以为会有一个简单的解决方案,但作为一个初学者,我显然无法理解。对我来说,最明显的事情是在用户输入失败后,以某种方式清除或清空分配给“choice”的值。可能吗 import java.util.Scanner; public class Furniture2Test { public static
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
}
import java.util.Scanner;
公共级家具2测试{
公共静态void main(字符串[]args){
木();
}//末端总管
公共静态空隙木材(){
智力选择;
int-pine=1;
int=2;
int桃花心木=3;
成本=100;
int-oakCost=225;
国际桃花心木成本=310;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“您想要什么类型的表?”);
System.out.println(“1.pine”);
System.out.println(“2.oak”);
System.out.println(“3.桃花心木”);
choice=keyboard.nextInt();
如果(选项==1){
选择=成本;
}else if(选项==2){
choice=oakCost;
}else if(选项==3){
选择=桃花心木成本;
}否则如果(选项>3 | |选项<1){
System.out.println(“重试”);
选择=-1;
木();
}
System.out.println(“这将是$”+choice+);
大小(选择);
}//尾木
公共静态空隙大小(整数选择){
国际大教堂;
int大=35;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“那是什么尺寸?”);
System.out.println(“1.large”);
System.out.println(“2.small”);
sizeChoice=keyboard.nextInt();
如果(sizeChoice==1)
println(“这将是$”+(选择+大型)+”);
else if(sizeChoice==2)
System.out.println(“这将是$”+选项);
其他的
System.out.println(“请输入1或2”);
}//末端尺寸
}
//将菜单逻辑
而(选项>3 | |选项<1){
//把你的重试逻辑。
}
//如果数字为1、2或3,则只能退出while循环,因此请将输出语句放在while循环之后
导入java.util.Scanner;
公共级家具2测试
{
公共静态void main(字符串[]args)
{
木();
}//末端总管
公共静态空隙木材()
{
智力选择;
int-pine=1;
int=2;
int桃花心木=3;
成本=100;
int-oakCost=225;
国际桃花心木成本=310;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“您想要什么类型的表?”);
System.out.println(“1.pine”);
System.out.println(“2.oak”);
System.out.println(“3.桃花心木”);
选择=读取范围(键盘,1,3);
如果(选项==1)
{
选择=成本;
}
其他的
如果(选项==2)
{
choice=oakCost;
}
其他的
如果(选项==3)
{
选择=桃花心木成本;
}
其他的
如果(选项>3 | |选项<1)
{
System.out.println(“重试”);
选择=-1;
木();
}
System.out.println(“这将是$”+choice+);
大小(选择);
}//尾木
公共静态空隙大小(整数选择)
{
国际大教堂;
int大=35;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“那是什么尺寸?”);
System.out.println(“1.large”);
System.out.println(“2.small”);
sizeChoice=读取范围(键盘,1,2);
如果(sizeChoice==1)
println(“这将是$”+(选择+大型)+”);
其他的
如果(sizeChoice==2)
System.out.println(“这将是$”+选项);
其他的
System.out.println(“请输入1或2”);
}//末端尺寸
专用静态整型读取范围(扫描仪、整型低、整型高){
int值;
值=scanner.nextInt();
而(值<低| |值>高){
系统输出打印(“请输入一个介于“+低+”和“+高+”之间的值:”;
值=scanner.nextInt();
}
返回值;
}
}//结束类
他们选择的任何数字都会存储在“choice”变量中,并使程序在应该停止时继续运行//
程序正在继续运行,因为您正在调用wood()if(choice>3 | | choice<1)
如果希望它停止,请删除wood()调用
如果您还想清除choice的值(而不是-1),可以将其赋值为null
choice
是方法wood的局部变量,当用户做出错误的选择时,您正在递归调用wood
。这是一个有趣的设计选择,在这种情况下可能不是最好的
当您再次调用wood
时,选择是rest(在此过程中为未知值赋值,直到用户为其赋值)
现在,当wood
方法存在时,问题就会出现……每次它返回调用方时,它都会调用size(choice)
,其中choice
是-1
(因为在再次调用wood之前,您将其设置为该值)
while循环
而不是递归调用size(choice)
查看更多详细信息使用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;
}
do{
System.out.println(“在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 = 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
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;
}