5

我正在创建一个小算法,这是其中的一部分。

如果用户输入非整数值,我想输出一条消息,让用户再次输入一个数字:

boolean wenttocatch;

do 
{
    try 
    {
        wenttocatch = false;
        number_of_rigons = sc.nextInt(); // sc is an object of scanner class 
    } 
    catch (Exception e) 
    {
        wenttocatch=true;
        System.out.println("xx");
    }
} while (wenttocatch==true);

我得到了一个永无止境的循环,我不知道为什么。

如何识别用户是否输入了一些非整数?
如果用户输入了一个非整数,如何让用户重新输入?

更新
当我打印异常时,我得到“InputMismatchException”,我该怎么办?

4

7 回答 7

5

Scanner读取项目之前,不会前进。这在Scanner JavaDoc中提到。因此,您可以使用方法读取值或在读取值之前.next()检查是否。hasInt()int

boolean wenttocatch;
int number_of_rigons = 0;
Scanner sc = new Scanner(System.in);

do {
    try {
        wenttocatch = false;
        number_of_rigons = sc.nextInt(); // sc is an object of scanner class
    } catch (InputMismatchException e) {
        sc.next();
        wenttocatch = true;
        System.out.println("xx");
    }
} while (wenttocatch == true);
于 2015-09-15T18:37:24.940 回答
5

你不必尝试捕捉。此代码将为您解决问题:

public static void main(String[] args) {
    boolean wenttocatch = false;
    Scanner scan = new Scanner(System.in);
    int number_of_rigons = 0;
    do{
        System.out.print("Enter a number : ");
        if(scan.hasNextInt()){
            number_of_rigons = scan.nextInt();
            wenttocatch = true;
        }else{
            scan.nextLine();
            System.out.println("Enter a valid Integer value");
        }
    }while(!wenttocatch);
}
于 2015-09-15T18:51:04.157 回答
0

任何时候遇到异常,wenttocatch设置为true并且程序将陷入无限循环。只要你没有得到异常,你就不会得到无限循环。

于 2015-09-15T18:31:19.020 回答
0

如果 sc.nextInt() 导致错误的逻辑是这样的

1) gotocatch 设置为 false

2) sc.nextInt() 抛出错误

3) gotocatch 设置为 true

4) 重复[因为 gotocatch 是真的]

在 catch 语句中解决这个 set goocatch=false

catch (Exception e) {
               wenttocatch=false;
               System.out.println("xx");
            }

如果您做的比这里显示的多,请使用计数器[如果您的计数或布尔值,如果您不是],但除非您做的更多,否则请执行上面的第一件事

boolean wenttocatch;
int count = 0;

            do{


             try {
                 wenttocatch=false;
                number_of_rigons=sc.nextInt(); // sc is an object of scanner class 
            } catch (Exception e) {
               count++;
               wenttocatch=true;
               System.out.println("xx");

            }

            }while(wenttocatch==true && count < 1);

回复评论:

我认为您想获得整数,直到用户不再输入。根据您的输入,一种方法是这样

int number_of_rigons;
while((number_of_rigons = sc.nextInt()) != null){
    //do something
}
于 2015-09-15T18:33:46.523 回答
0

您只需使用 hasNext(); java中的方法,而不是try and catch。hasNext() 是 Java Scanner 类的一个方法,如果此扫描器的输入中有另一个标记,则返回 true。

于 2021-08-04T06:08:18.340 回答
0
 String[] stringArray = new String[lines];
   int i = 0;
   try (Scanner sc = new Scanner(file)) {
       while (sc.hasNextLine()) {
           String data=sc.nextLine();
           stringArray[i] = data;

           i++;
       }
   } catch (FileNotFoundException fileNotFoundException) {
       System.err.println("Error opening file.");
       throw new FileNotFoundException();
   } catch (NoSuchElementException e) {
       System.err.println("Error in file record structure");
       throw new NoSuchElementException();
   } catch (Exception e) {
       e.printStackTrace();
   }
  
于 2021-12-13T16:42:02.547 回答
-1
....
try {
  ....
} catch (Exception e) {
  sc.next();
  wenttocatch=true;
  System.out.println("xx");
}
于 2018-02-08T04:25:51.253 回答