Java 如何防止扫描程序接受字符串、负整数或小于2的数字?

Java 如何防止扫描程序接受字符串、负整数或小于2的数字?,java,try-catch,java.util.scanner,Java,Try Catch,Java.util.scanner,如何防止此方法返回负数? 我已尝试将while循环设置为 (n < 0 && n != 0) (n=0&&n最终代码不返回负整数或字符串: public int getNumber() { System.out.print("Enter the upper bound(0 to exit): "); int nums = 1; while(true) { while(!scan.hasNextInt()) {

如何防止此方法返回负数? 我已尝试将while循环设置为

(n < 0 && n != 0)
(n<0&&n!=0)
无济于事。 以下是我当前使用的方法的代码:

public int getNumber() {          
       int n = 1;
       while(n < 2 && n != 0) {
           if(n < 0) {
               System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");               
               scan.next();
               n = scan.nextInt();
           }
       try {           
           System.out.print("Enter the upper bound(0 to exit): ");
           n = scan.nextInt();           
           break;
       }
       catch(java.util.InputMismatchException e) {
           System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");
           scan.next();
           continue;
           }      
       }
       return n;      
   } 
public int getNumber(){
int n=1;
而(n<2&&n!=0){
if(n<0){
System.out.print(“错误,请输入大于0的有效数字(0退出):”;
scan.next();
n=scan.nextInt();
}
试试{
系统输出打印(“输入上限(0退出):”;
n=scan.nextInt();
打破
}
catch(java.util.InputMismatchException e){
System.out.print(“错误,请输入大于0的有效数字(0退出):”;
scan.next();
继续;
}      
}
返回n;
} 
我还尝试将if语句放入try块中,如下所示:

public int getNumber() {          
       int n = 1;
       while(n < 2 && n != 0) {

       try {           
           System.out.print("Enter the upper bound(0 to exit): ");
           n = scan.nextInt();
           if(n < 0) {
               System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");               
               scan.next();
               n = scan.nextInt();
           }
           break;
       }
       catch(java.util.InputMismatchException e) {
           System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");
           scan.next();
           continue;
           }      
       }
       return n;      
   }
System.err.println("Enter a number between 0 and 5, inclusive");
int res = -1;
while (true) {
    while (!scan.hasNextInt()) {
        System.err.println("Incorrect input. Please enter a number between 0 and 5, inclusive");
        scan.nextLine(); // Discard junk entries
    }
    res = scan.nextInt();
    if (res >= 0 && res <= 5) {
        break;
    }
    System.err.println("Invalid number. Please enter a number between 0 and 5, inclusive");
}
// When you reach this point, res is between 0 and 5, inclusive
public int getNumber(){
int n=1;
而(n<2&&n!=0){
试试{
系统输出打印(“输入上限(0退出):”;
n=scan.nextInt();
if(n<0){
System.out.print(“错误,请输入大于0的有效数字(0退出):”;
scan.next();
n=scan.nextInt();
}
打破
}
catch(java.util.InputMismatchException e){
System.out.print(“错误,请输入大于0的有效数字(0退出):”;
scan.next();
继续;
}      
}
返回n;
}

当我将if语句放入try块时,我开始连续输入负数进行测试。我第一次输入一个负数,然后给我一个空白的扫描仪输入行,最后让一个负数返回,这反过来又把我程序的其余部分搞砸了。请帮忙,我是爪哇第一学期的学生。谢谢。

你输入一个负数,然后它进入你的n你输入一个负数,然后它进入你的n不要在验证输入时使用
循环条件。在决定保留或拒绝输入值之前,循环条件不会给程序提供接受和检查数字的机会。因此,您的程序甚至在最终用户键入任何内容之前就开始向他们提示错误消息

如果未先检查
扫描仪
是否已准备好通过调用
hasnetint
向您提供
int
,则不应调用
nextInt

最后,您需要一个拒绝循环来丢弃非整数输入,直到
hasnetint
成功。这通常通过嵌套的
while
循环完成,该循环打印错误提示,并丢弃输入的值

读取和验证
int
的总体框架如下所示:

public int getNumber() {          
       int n = 1;
       while(n < 2 && n != 0) {

       try {           
           System.out.print("Enter the upper bound(0 to exit): ");
           n = scan.nextInt();
           if(n < 0) {
               System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");               
               scan.next();
               n = scan.nextInt();
           }
           break;
       }
       catch(java.util.InputMismatchException e) {
           System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");
           scan.next();
           continue;
           }      
       }
       return n;      
   }
System.err.println("Enter a number between 0 and 5, inclusive");
int res = -1;
while (true) {
    while (!scan.hasNextInt()) {
        System.err.println("Incorrect input. Please enter a number between 0 and 5, inclusive");
        scan.nextLine(); // Discard junk entries
    }
    res = scan.nextInt();
    if (res >= 0 && res <= 5) {
        break;
    }
    System.err.println("Invalid number. Please enter a number between 0 and 5, inclusive");
}
// When you reach this point, res is between 0 and 5, inclusive
System.err.println(“输入一个介于0和5之间的数字,包括0和5”);
int res=-1;
while(true){
而(!scan.hasnetint()){
System.err.println(“输入不正确。请输入一个介于0和5之间的数字,包括0和5”);
scan.nextLine();//丢弃垃圾条目
}
res=scan.nextInt();

如果(res>=0&&res在验证输入时不要使用
循环条件。在决定保留或拒绝输入值之前,循环条件不会给程序提供接受和检查数字的机会。因此,即使在最终用户键入任何内容之前,程序也会开始提示用户错误消息

如果未先检查
扫描仪
是否已准备好通过调用
hasnetint
向您提供
int
,则不应调用
nextInt

最后,您需要一个拒绝循环来丢弃非整数输入,直到
hasnetint
成功。这通常通过嵌套的
while
循环完成,该循环打印错误提示,并丢弃输入的值

读取和验证
int
的总体框架如下所示:

public int getNumber() {          
       int n = 1;
       while(n < 2 && n != 0) {

       try {           
           System.out.print("Enter the upper bound(0 to exit): ");
           n = scan.nextInt();
           if(n < 0) {
               System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");               
               scan.next();
               n = scan.nextInt();
           }
           break;
       }
       catch(java.util.InputMismatchException e) {
           System.out.print("Error, please enter a valid number greater than 0(0 to exit): ");
           scan.next();
           continue;
           }      
       }
       return n;      
   }
System.err.println("Enter a number between 0 and 5, inclusive");
int res = -1;
while (true) {
    while (!scan.hasNextInt()) {
        System.err.println("Incorrect input. Please enter a number between 0 and 5, inclusive");
        scan.nextLine(); // Discard junk entries
    }
    res = scan.nextInt();
    if (res >= 0 && res <= 5) {
        break;
    }
    System.err.println("Invalid number. Please enter a number between 0 and 5, inclusive");
}
// When you reach this point, res is between 0 and 5, inclusive
System.err.println(“输入一个介于0和5之间的数字,包括0和5”);
int res=-1;
while(true){
而(!scan.hasnetint()){
System.err.println(“输入不正确。请输入一个介于0和5之间的数字,包括0和5”);
scan.nextLine();//丢弃垃圾条目
}
res=scan.nextInt();

如果(res>=0&&res不能检查'hasNextInt',则测试输入

int n = 0;
System.out.println("Enter a number between 0 and 5);
while (scan.hasNextInt()) {
    n = scan.nextInt();
    if (n >= 0 && n <= 5) {
        break;
    }else{
        //prompt error message or handle however you wish
    }
}
return n;
int n=0;
System.out.println(“输入一个介于0和5之间的数字);
while(scan.hasNextInt()){
n=scan.nextInt();

如果(n>=0&&n不能检查'hasNextInt',则测试输入

int n = 0;
System.out.println("Enter a number between 0 and 5);
while (scan.hasNextInt()) {
    n = scan.nextInt();
    if (n >= 0 && n <= 5) {
        break;
    }else{
        //prompt error message or handle however you wish
    }
}
return n;
int n=0;
System.out.println(“输入一个介于0和5之间的数字);
while(scan.hasNextInt()){
n=scan.nextInt();

如果(n>=0&&n最终代码不返回负整数或字符串:

public int getNumber() {
       System.out.print("Enter the upper bound(0 to exit): ");
       int nums = 1;
       while(true) {
           while(!scan.hasNextInt()) {
               System.out.print("Error. Please enter a valid integer greater than 1(0 to exit): ");
               scan.nextLine();
           }
           nums = scan.nextInt();
           if(nums > 2 || nums == 0) {
               break;
           } else {
               System.out.print("Error. Please enter a valid integer greater than 1(0 to exit): ");
               scan.nextLine();
           }
       }
      return nums;
   }

非常感谢大家!

不返回负整数或字符串的最终代码:

public int getNumber() {
       System.out.print("Enter the upper bound(0 to exit): ");
       int nums = 1;
       while(true) {
           while(!scan.hasNextInt()) {
               System.out.print("Error. Please enter a valid integer greater than 1(0 to exit): ");
               scan.nextLine();
           }
           nums = scan.nextInt();
           if(nums > 2 || nums == 0) {
               break;
           } else {
               System.out.print("Error. Please enter a valid integer greater than 1(0 to exit): ");
               scan.nextLine();
           }
       }
      return nums;
   }
非常感谢你们