【发布时间】:2015-03-17 20:38:32
【问题描述】:
该方法应该返回用户输入的整数,只要它只是一个整数(不是字符串、浮点数等)并且该整数是给定选项列表中的选项之一.每当我向用户提供他们需要从中选择的选项列表时,我都想在整个程序中使用此方法。这些列表将具有不同的大小,因此我将用户可能选择的最大值 (maxValue) 作为参数传递,从而为该方法提供列表的大小。
//This method handles the players input and checks if the number entered is one of the options listed or not
public static int response(int maxValue){ //the parameter is the number of options in the particular list
response = new Scanner(System.in);
Boolean check = true;
while(check){
try{
int yesOrNo = response.nextInt();
if(yesOrNo > maxValue || yesOrNo <= 0){ //checks if the int entered does not exceed the list size or go below zero
System.out.println("I'm sorry, that number was not one of the options. Please reselect your choice.");
}else{
check = false;
}
}catch(Exception e){ //catches an exception when the user enters a string or anything but an int
System.out.println("Please only use digits to make a selection.");
response(maxValue);
}
}
return yesOrNo; //returns the user's response. Well, it is supposed to.
}
我是编程方面的初学者。我正在通过在线教程和我制作的愚蠢的小程序反复试验来学习 Java。我正在制作一个有趣的小文字冒险游戏,但仍处于起步阶段。
我遇到的麻烦是因为这个方法只会返回0。yesOrNo不是被分配了用户通过扫描仪response输入的整数吗?为什么只返回 0?
感谢您的回复。我现在明白我需要在try 之外声明我的int yesOrNo,因为正如你们所说的那样,它超出了范围,被声明在其中。
但有些人提到“在 catch 块中有一个完全不必要的函数调用”。唯一的问题是如果我删除它,当用户输入字符串或其他非整数值时,System.out.println("Please only use digits to make your selection.") 会创建一个无限循环。
这是我更新的代码:
//This method handles the players input and checks if the number entered is one of the options listed or not
public static int response(int maxValue){ //the parameter is the number of options in the particular list
response = new Scanner(System.in);
Boolean check = true;
int yesOrNo = 0;
while(check){
try{
yesOrNo = response.nextInt();
if(yesOrNo > maxValue || yesOrNo <= 0){ //checks if the int entered does not exceed the list size or go below zero
System.out.println("I'm sorry, that number was not one of the options. Please reselect your choice.");
}else{
check = false;
}
}catch(Exception e){ //catches an exception when the user enters a string or anything but an int
System.out.println("Please only use digits to make a selection.");
response(maxValue);
}
}
return yesOrNo; //returns the user's response. Well, it is supposed to.
}
在问另一个问题之前阅读其他帖子后,我发现许多其他人面临同样的问题。有些人说创建了无限循环是正确的,因为当 Scanner 遇到错误时,它不会删除该错误的标记,从而导致 while 循环无限地再次读取相同的错误。这是我读到的内容:
“根据扫描仪的 javadoc:
'当扫描器抛出 InputMismatchException 时,扫描器不会传递导致异常的令牌,以便可以通过其他方法检索或跳过它。'
这意味着如果下一个标记不是 int,它会抛出 InputMismatchException,但标记会保留在那里。因此,在循环的下一次迭代中,getAnswer.nextInt() 再次读取相同的标记并再次抛出异常。你需要的是用完它。在你的 catch 中添加一个 getAnswer.next() 来消耗令牌,它是无效的,需要被丢弃。”
所以现在无限循环问题已经解决了 :) 继续寻找我还需要学习的内容。谢谢。
【问题讨论】:
-
你的整个 try/catch 都在一个 while 循环内,为什么你要在循环内再次调用 response?这不是必需的,因为循环应该会自动重复。此外,在循环外声明您的 int yesOrNo ,然后读取 try 块中的值。这在内存上更好,因为每次循环运行时都不会重新声明 int。您还应该确保在退货声明之前关闭扫描仪。
-
java 中未赋值的整数自动赋值为零。有用的事实。
-
没有人提到在catch块中有一个完全不必要的函数调用?
-
@EvanBechtol 但是如果没有该声明,当方法捕获异常时会创建一个无限循环。
-
@BrandonVance 不,您的程序布局方式应该再次通过 try/catch 进行,因为您的“检查”值仍设置为 true。您很可能在某个地方遇到了涉及您的 response.nextInt(); 的错误。它将捕获上一个条目中剩余的空终止符。
标签: java methods return-value