繁体   English   中英

用户输入时执行Do-While循环-检查输入是否为空或int(java)

[英]Do-While loop on user input - check if input is not empty or int (java)

我希望用户被反复询问一个问题,即用户输入是否为空或不是int。 这是我到目前为止的内容:

        Scanner scn = new Scanner(System.in);
        do {
            System.out.print("Enter id: ");
            int id = scn.nextInt();
         } while (!scn.hasNextInt());

简单地说:

while (scanner.hasNextInt());

尝试这样的事情:

布尔值运行= true;

while(running){
    String s = scn.nextLine();
    if(!(s!=""||isInt(s))){
        running = false;
    }
}

您需要实现自己的isInt()函数,但某些Google会为您提供此功能。

试试这个删除!

while (scn.hasNextInt());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM