簡體   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