簡體   English   中英

|| 在while循環中無法按預期工作

[英]|| in while loop not working as expected

我有一個似乎無法解決的問題。 我的目標是提示用戶玩游戲的人數。 我要在用戶輸入滿足以下要求時繼續提示用戶:-輸入不是整數-輸入小於3-輸入大於7

發生的情況是,似乎需要3個輸入來檢查第三個條件,第一個條件檢查一個條件,第二個條件檢查兩個條件。 它不會一次檢查所有內容,並且我認為它與語法.nextInt()有關,但是我找不到另一種表達方式。 提前謝謝您的幫助!

這是代碼:

public void setNumPlayers(Game game) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");
    while(!input.hasNextInt() || input.nextInt() < 3 || input.nextInt() > 7) {
        System.out.println("Please eneter the number of people playing. You must have at least three players, and no more than seven.");
        input.next();
    }
    game.setNumPlayers(input.nextInt());
    input.close();
}

和主要的電話

Game g = new Game();
    ConsoleOutput io = new ConsoleOutput();
    io.setNumPlayers(g);
    System.out.println(g.getNumPlayers());

編輯:我已更改代碼以將nextInt存儲為變量。 以下代碼有效,如果我輸入字母之外的數字,則提示我,提示我並讓我重新分配x,唯一的問題是如果我輸入了錯誤的數字,然后輸入字母,則會崩潰...我應該將其封裝在某種嘗試/捕獲中嗎? 那似乎不切實際。

public void setNumPlayers(Game game) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");     

    while(!input.hasNextInt()) {            
        System.out.println("Please eneter the number of people playing.");
        input.next();
    }
    int x = input.nextInt();
    while(x<3 || x>7) {
        System.out.println("The number of players must be at least three and no more than seven.");
        x = input.nextInt();              
    }

    game.setNumPlayers(x);
    input.close();
}

通過兩次調用Scanner.nextInt(),您將讀取兩個數字,而不是兩次讀取相同的數字。

考慮讀取int值,將其存儲在變量中,然后在if語句中使用它。

如果要檢查輸入並繼續要求輸入直到輸入良好,則必須修改一點邏輯。 以下將適用於一系列輸入,例如:

How many people are playing?
Please enter the number of people playing. You must have at least three players, and no more than seven.
10
Please enter the number of people playing. You must have at least three players, and no more than seven.
toto
Please enter the number of people playing. You must have at least three players, and no more than seven.
tutu
Please enter the number of people playing. You must have at least three players, and no more than seven.
22
Please enter the number of people playing. You must have at least three players, and no more than seven.
6


    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");

    int numPlayers = 0;
    while (numPlayers < 3 || numPlayers > 7) {
        System.out.println("Please enter the number of people playing. You must have at least three players, and no more than seven.");
        if (!input.hasNextInt()) {
            input.next();
        }
        else {
            numPlayers = input.nextInt();
        }
    }
    game.setNumPlayers(numPlayers);
    input.close();

使用&&運算符。 並使用局部變量並從掃描儀分配值。 並適當使用它。

int x =0;
 if(input.hasNextInt())
 {
 x= input.nextInt();
 }
 while( x > 3 && x < 7) {
    System.out.println("Please eneter the number of people playing. You must have at least three players, and no more than seven.");
   input.next();
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM