Java 不确定如何正确使用该方法
我想使用.nextInt从扫描仪获取输入并将其发送到下面的方法,但我不确定如何进行Java 不确定如何正确使用该方法,java,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.
// 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
}
您的方法返回整数值,因此请将其指定给变量,或按您希望的方式使用。我对扫描仪部分感到困惑,正在尝试checkValidInputscan.nextInt;
int age = checkValidInput(scan);