【发布时间】:2013-09-25 05:19:30
【问题描述】:
如果输入不是整数,我正在尝试使用 while 循环要求用户重新输入
例如。输入是任何浮点数或字符串
int input;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter the number of miles: ");
input = scan.nextInt();
while (input == int) // This is where the problem is
{
System.out.print("Invalid input. Please reenter: ");
input = scan.nextInt();
}
我想不出办法来做到这一点。我刚刚被介绍给java
【问题讨论】:
-
看看
Scanner.hasNext方法。在那里您可以确定下一个输入是哪种类型。
标签: java