簡體   English   中英

如果條件為真,則繼續 while 循環

[英]continue a while loop if a condition is true

我有一個猜謎游戲程序,您有 4 次嘗試猜測 0 到 9 之間的數字。我想添加一個選項,當用戶超過 4 次嘗試時,程序會詢問用戶是否要再次玩游戲。 這是我的代碼:

public static void main(String[] args) {
    int nb, x;
    int count = 1;
    Scanner inp = new Scanner(System.in);
    nb = (int) (Math.random() * 10);

    System.out.print("Guess the number between 0 and 9 (4 Guesses allowed):");
    x = inp.nextInt();

    while (x != nb) {
        if (count <= 3) {
            if (x > nb) System.out.println("Lesser");
            else if (x < nb) System.out.println("Bigger");
            System.out.println("Wrong try another number");
            x = inp.nextInt();
            if (x == nb) System.out.println("Found!!");
        } else {
            System.out.print("you exceeded your allowed Guesses");
            break;
        }
        count++;
    }
    inp.close();
}

希望您(和其他所有人)都保持健康!

嵌套在您的 while 循環中,您有一個 if-else 語句。 這是我建議您添加的地方。 在 else 分支中,我會輸出您想要的消息,然后處理用戶輸入:如果用戶說是,則將您的計數整數重置為零; while 循環將重新啟動,您將能夠繼續猜測。 如果用戶說不,我會像你現在一樣執行 break 語句。 希望這可以幫助!

看起來是一個很好的候選者,可以使用單獨的方法。 將當前循環移動到另一個方法,然后在 main 方法中創建另一個循環,該循環調用新創建的方法並在播放器運行后返回時提示重播。

部分代碼示例:

public static void main(String[] args) {
  Scanner inp = new Scanner(System.in);
  boolean play = true;
  while(play) {
    runGame(inp);
    System.out.println("Play again?");
    play = inp.nextLine().toUpperCase().contains("YES");
  }
  inp.close();
}

public static void runGame(Scanner inp) {
  int count = 1;
  //Move your current loop stuff here
}

解決方案

在控制流結束時,無論是您猜到了正確答案還是超過了猜測次數,您都需要提示用戶進行輸入。

也許是這樣的:“你想再試一次嗎?”

您可以使用掃描器庫來執行此操作,您可以從此處閱讀並實施它。

如果他們輸入“是” (注意大小寫)

分配nb = (int) (Math.random() * 10); 再次驗證它與以前的值不同。 這將導致循環繼續運行,從而游戲繼續。

注意:重要的是,如果再次出現相同的數字,請處理它以免終止游戲。 您可以通過獲取另一個隨機數 != 到前一個,或從您的隨機數池中排除該前一個數字來確保選擇不同。

此外,出於同樣的原因,我建議您為變量提供更好的名稱以提高可讀性,並更好地格式化代碼。

我希望這有幫助。

只需稍作調整,這將使您繼續前進。

    public static void main(String[] args) {
int nb, x=-1;
int count = 0;
Scanner inp = new Scanner(System.in);
nb = (int) (Math.random() * 10);


while (x != nb) {
    System.out.print("Guess the number between 0 and 9 (4 Guesses allowed):");
    x = inp.nextInt();
        inp.nextLine();
         count++;
    if (x == nb){ System.out.println("Found!!");break;}
    if (x > nb) System.out.println("Lesser");
        else if (x < nb) System.out.println("Bigger");
        System.out.println("Wrong try another number");

    if (count == 2) {


        System.out.println("you exceeded your allowed Guesses");
        System.out.println("would you like to play again? (input y or n)");
        String newGame = inp.next();
               inp.nextLine();

        if(newGame.compareTo("n")==0)break;
        else count = 0;  

    }

}
inp.close();
}
}
public static void main(String... args) {
    final int min = 0;
    final int max = 10;
    final int maxGuesses = 4;

    try (Scanner scan = new Scanner(System.in)) {
        while (true) {
            final int expected = min + new Random().nextInt(max + 1 - min);
            boolean found = false;

            System.out.printf("Guess the number between %d and %d (%d Guesses allowed).\n", min, max, maxGuesses);

            for (int i = 1; i <= maxGuesses; i++) {
                System.out.printf("Guess #%d: ", i);
                int number = scan.nextInt();

                if (number == expected) {
                    found = true;
                    break;
                }

                System.out.println(number > expected ? "Lesser" : "Bigger");

                if (i < maxGuesses)
                    System.out.println("Wrong try another number");
            }

            System.out.println(found ? "Found!!" : "you exceeded your allowed Guesses");

            System.out.print("Do you want to continue (Y/N): ");
            char ch = scan.next().charAt(0);

            if (Character.toLowerCase(ch) == 'N')
                break;
        }
    }
}

暫無
暫無

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

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