它现在所做的是要求用户输入一个字段编号,但如果用户输入一个非整数,它只会陷入循环并且不会响应用户键入的任何内容。我希望它真正起作用,我认为它在我的 try/catch 中。谢谢
代码:
public static int getFieldNumberFromUser(int maxValue)throws IOException
{
BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String data;
int fieldNumber;
boolean isInteger;
fieldNumber = 0;
isInteger = false;
while(isInteger == false)
{
System.out.print("Enter a field between 1 and " + maxValue + "(< zero to quit): ");
data = br.readLine();
try
{
fieldNumber = Integer.parseInt(data);
isInteger = true;
}
catch(NumberFormatException nfe)
{
System.out.println("You did not enter a valid integer");
}
}
return fieldNumber - 1;
}