Java try-catch块中的变量范围
我正在通过命令行创建一个用户界面,该界面将要求1-4中的选项,我希望进行错误检查。只允许1到4之间的整数。这是我目前掌握的代码。我希望该方法将userInput整数返回给另一个方法,该方法将对其执行一些操作Java try-catch块中的变量范围,java,variables,scope,try-catch,console-application,Java,Variables,Scope,Try Catch,Console Application,我正在通过命令行创建一个用户界面,该界面将要求1-4中的选项,我希望进行错误检查。只允许1到4之间的整数。这是我目前掌握的代码。我希望该方法将userInput整数返回给另一个方法,该方法将对其执行一些操作 package contactmanager; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author andyjohnson */ public class UserIn
package contactmanager;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author andyjohnson
*/
public class UserInterface {
public static Integer GetInput() {
Scanner in = new Scanner(System.in);
//Integer userInput;
System.out.println("Welcome to the contact manager\nMake a selection below:");
System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit");
try {
Integer userInput = in.nextInt();
if (userInput < 1 || userInput > 4) {
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
} catch (InputMismatchException e) {
e.getMessage();
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
return userInput;
}
}
我的return语句在IDE中有下划线,它告诉我它没有初始化。我想全局初始化它,但允许try语句更改该值。我已经试过了。userInput=userInput,但是我不知道我的作用域在哪里被破坏了。如何赋予try块全局范围?我是java新手,所以什么都有帮助。谢谢 您可以在try-catch块之外声明userInput变量:
package contactmanager;
import java.util.InputMismatchException;
import java.util.Scanner;
public class UserInterface {
public static Integer GetInput() {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the contact manager\nMake a selection below:");
System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit");
Integer userInput = null;
try {
userInput = in.nextInt();
if (userInput < 1 || userInput > 4) {
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
} catch (InputMismatchException e) {
e.getMessage();
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
return userInput;
}
}
try-catch块的作用域与只编写与方法其余部分一致的代码不同
我认为您遇到的问题是在这一行中,变量userInput首先在try-catch块中声明:
package contactmanager;
import java.util.InputMismatchException;
import java.util.Scanner;
public class UserInterface {
public static Integer GetInput() {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the contact manager\nMake a selection below:");
System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit");
Integer userInput = null;
try {
userInput = in.nextInt();
if (userInput < 1 || userInput > 4) {
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
} catch (InputMismatchException e) {
e.getMessage();
System.out.println("Please enter a valid selection");
UserInterface.GetInput();
}
return userInput;
}
}
这是一个问题的原因:
考虑try-catch块是否失败。然后会返回什么?变量userInput甚至还没有定义,所以Java不知道返回什么
解决方法相对简单。你只想把它移出try-catch块,就像这样。这将消除返回错误。我注意到你把这一变化注释掉了。为什么?
但我还有一个建议。为什么要调用UserInterface.GetInput?为什么不让方法接受有效输入的参数,并且在数据格式不正确时不调用它呢?你用它吗?这样就不需要真正的全局作用域变量
由于该方法的编写方式,它必须返回某种类型的整数,除非您编写它,否则该方法会引发在下游某处捕获的异常
我试图做一些我认为最有意义的修复:
Scanner in = new Scanner(System.in);
Integer userInput; // Integer representation of user input
try {
Integer a = in.nextInt(); // if a can be assigned to an Integer this line work
if (a < 1 || a > 4) {
// called if the input can be assigned to an Integer and is within the range
userInput = a;
}
} catch (InputMismatchException e) {
// otherwise the catch block is called
System.out.println("Please enter a valid selection");
}
return userInput;
我需要根据规范再次调用UserInterface.GetInput。如果输入验证失败,程序需要再次提供菜单。我尝试了这个建议,但它似乎没有验证输入整数是否为0或5。输入像foo这样的字符串,我得到了catch语句中的代码。如果try验证失败或catch执行,那么再次调用菜单方法的最佳方式是什么?规范说,输入验证失败后,这将为用户提供菜单。我想让UserInterface方法成为静态的,这样就不需要实例化。我不知道您正在查看的“规范”中列出了哪些条件。我测试了这个方法,它似乎对我有效。我不明白UserInterface.GetInput与它有何关联,因为您可能已经获得了输入。为了清晰起见,我在原始答案中添加了注释。如果输入失败,我需要询问正确的输入。最好的方法是再次调用整个方法,还是有更好的方法?基本上,在用户做任何事情之后,我需要它返回主菜单。哦,我现在明白了。你所描述的是通过所谓的哨兵旗来实现的。等一下,让我来编辑我的帖子。
Scanner input = new Scanner(System.in);
System.out.println("Please enter a valid Integer. When done type DONE ");
// this method will keep cycling until the String DONE is typed
// you could make this condition whatever you want
while (!input.hasNext("DONE")) {
String a = input.next(); // gets the next item from the Scanner
try {
Integer b = Integer.parseInt(a); // tries to 'cast' the String to an Integer
if (b < 1 || b > 4) {
System.out.println("Input is valid!");
} else {
System.out.println("Input is invalid!");
}
} catch (NumberFormatException e) {
System.out.println("Please enter a valid selection!");
}
}