Java验证用户整数输入
我写的代码 1) 不断要求用户选择数字并检查输入是否为整数Java验证用户整数输入,java,java.util.scanner,Java,Java.util.scanner,我写的代码 1) 不断要求用户选择数字并检查输入是否为整数 private static int readInputInt(String error) { Scanner s = new Scanner(System.in); while (!s.hasNextInt()) { System.out.println(error); s.next(); } int result = s.nextInt(); //s.close
private static int readInputInt(String error) {
Scanner s = new Scanner(System.in);
while (!s.hasNextInt()) {
System.out.println(error);
s.next();
}
int result = s.nextInt();
//s.close();
return result;
}
2) 连续要求用户选择范围内的数字并仅显示错误,程序不显示消息要求用户再次提供输入
private static int readInputInt(String error, int max) {
Scanner s = new Scanner(System.in);
int result;
do {
while(!s.hasNextInt()) {
//show error if it is not integer and get input once again
System.out.println(error);
s.next();
}
result = s.nextInt();
// if result is integer check if it is bigger than max
if(result > max) {
System.out.println(error);
}
}while(result > max);
我想知道是否有更简单的方法可以做到这一点,因为我花了太多的时间,我认为这是一种垃圾的编码方式
首先,我认为以下代码可以工作:
private static int readInputInt(String error, int max) {
Scanner s = new Scanner(System.in);
while (!s.hasNextInt() && (s.nextInt < max)) {
System.out.println(error);
s.next();
}
int result = s.nextInt();
//s.close();
return result;
}
private static int readinput(字符串错误,int max){
扫描仪s=新的扫描仪(System.in);
而(!s.hasNextInt()&&(s.nextInt
但它不起作用。谢谢你的帮助 我在编写代码时多次遇到这个问题,通常最适合我的解决方案是使用 尝试/抓住 让我告诉你我的意思
private static int readInputInt(String error) {
Scanner s = new Scanner(System.in);
//1. Reading the input as a string.
String input = s.nextLine();
int result;
// A while loop that will keep going on until the user enters a
// valid integer.
while (true) {
// Try/catch block that tries to parse the string to an integer
try {
// If the user enters a valid integer there will be no problem
// parsing it. Otherwise, the program will throw a 'NumberFormatException'.
result = Integer.parseInt(input);
// If the parsing has been successful,
//we break the while loop and return the result
break;
}catch(NumberFormatException nfe) {
// If the user did not enter a valid integer we will just
// print the error message.
System.out.println(error);
}
// Read user input again and repeat the procedure above.
input = s.nextLine();
}
return result;
}
我希望这有帮助。如果您不熟悉try/catch,我建议您在线阅读。太好了 我在编写代码时多次遇到这个问题,通常最适合我的解决方案是使用
private static int readInputInt(String error, int max) {
Scanner s = new Scanner(System.in);
int result;
while (true) {
if (s.hasNextInt()) {
result = s.nextInt();
if (result <= max) {
s.close();
return result;
}
} else { //Only want to call next() if it doesn't meet the first conditional. We've already called next when it is an int.
s.next();
}
System.out.println(error);
}
尝试/抓住
让我告诉你我的意思
private static int readInputInt(String error) {
Scanner s = new Scanner(System.in);
//1. Reading the input as a string.
String input = s.nextLine();
int result;
// A while loop that will keep going on until the user enters a
// valid integer.
while (true) {
// Try/catch block that tries to parse the string to an integer
try {
// If the user enters a valid integer there will be no problem
// parsing it. Otherwise, the program will throw a 'NumberFormatException'.
result = Integer.parseInt(input);
// If the parsing has been successful,
//we break the while loop and return the result
break;
}catch(NumberFormatException nfe) {
// If the user did not enter a valid integer we will just
// print the error message.
System.out.println(error);
}
// Read user input again and repeat the procedure above.
input = s.nextLine();
}
return result;
}
我希望这有帮助。如果您不熟悉try/catch,我建议您在线阅读。太好了 private static int readinput(字符串错误,int max){
private static int readInputInt(String error, int max) {
Scanner s = new Scanner(System.in);
int result;
while (true) {
if (s.hasNextInt()) {
result = s.nextInt();
if (result <= max) {
s.close();
return result;
}
} else { //Only want to call next() if it doesn't meet the first conditional. We've already called next when it is an int.
s.next();
}
System.out.println(error);
}
扫描仪s=新的扫描仪(System.in);
int结果;
虽然(正确){
如果(s.hasNextInt()){
结果=s.nextInt();
if(resultprivate static int readinput(字符串错误,int max){
扫描仪s=新的扫描仪(System.in);
int结果;
虽然(正确){
如果(s.hasNextInt()){
结果=s.nextInt();
if(result)首先,您使用的是s.nextInt,这是一个函数,因此它应该是s.nextInt()。其次,在while循环中,您调用next()两次,这是不正确的。首先,您使用的是s.nextInt,这是一个函数,因此它应该是s.nextInt()。其次,在while循环中,您调用next()两次,这是不正确的。天哪!它工作了!不幸的是,我不能放置s.close();因为我有很多方法使用“Scanner s=new Scanner(System.in)”,当我放入任何“s.close()”然后输入已损坏。谢谢!如果您将scanner作为参数传入,这是有意义的。但是,如果您在每个方法中创建新的scanner,则会发生其他情况。无论如何,很高兴它对您有效!天哪!它有效!不幸的是,我无法放置s.close();因为我有许多正在使用的方法“Scanner s=new Scanner(System.in);”当我输入任何“s.close();”时,输入就被破坏了。谢谢!如果您将Scanner作为参数传入,这是有道理的。但是如果您在每个方法中创建一个新的Scanner,则会发生其他事情。无论如何,很高兴它为您解决了!非常好且清晰的解释“为什么?”“您已将此类代码放置在特定位置。谢谢!非常好且清晰的解释“为什么”您已将此类代码放置在特定位置。谢谢!