[英]Do-While loop on user input - check if input is not empty or int (java)
I want the user to be repeatedly asked a question if the user input is either empty OR not an int. 我希望用户被反复询问一个问题,即用户输入是否为空或不是int。 This is what I have so far:
这是我到目前为止的内容:
Scanner scn = new Scanner(System.in);
do {
System.out.print("Enter id: ");
int id = scn.nextInt();
} while (!scn.hasNextInt());
简单地说:
while (scanner.hasNextInt());
Try something like this: 尝试这样的事情:
boolean running = true; 布尔值运行= true;
while(running){
String s = scn.nextLine();
if(!(s!=""||isInt(s))){
running = false;
}
}
You need to implement your own isInt() function, but some google will give you this. 您需要实现自己的isInt()函数,但某些Google会为您提供此功能。
Try this removing !
试试这个删除
!
: :
while (scn.hasNextInt());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.