不确定如何正确使用方法

时间:2015-03-14 19:00:15

标签: java

我想使用.nextInt()从扫描仪获取输入并将其发送到下面的方法,但我不确定如何。

// Method to check the user has enter an integer
 static int checkValidInput ( Scanner scan)
{
    while (!scan.hasNextInt()) // 
    {
        scan.next(); // if the input is not an integer it is discarded.

        System.out.println("You did not enter a integer"); // the question is asked again until the right value is entered
    }   

    return scan.nextInt(); // the integer entered is returned to the statement that called the method
}

1 个答案:

答案 0 :(得分:0)

int age = checkValidInput(scan);