Java 做 while 循环猜猜游戏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33662244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:53:24  来源:igfitidea点击:

Do while loop for guessing game

javado-while

提问by decken

I am writing a simple java guessing game, the game is supposed to randomly pick a number between 1 and 100 and then after you pick the correct number it asks you if you would like to play again. I know i should be able to use a Do while loop to ask if the user would like to play again, but every time I try I cannot make it work. This is my fully functional program without the do while loop. If someone could help me prompt the user if they would like to play again I would be very thankful. I am still very new to programming. Also I apologize if the indenting isn't 100% correct.

我正在编写一个简单的 java 猜谜游戏,该游戏应该随机选择 1 到 100 之间的一个数字,然后在您选择正确的数字后,它会询问您是否想再玩一次。我知道我应该能够使用 Do while 循环来询问用户是否想再次播放,但是每次我尝试都无法使其工作。这是我没有 do while 循环的全功能程序。如果有人能帮我提示用户是否愿意再次玩,我将非常感激。我对编程还是很陌生。如果缩进不是 100% 正确,我也深表歉意。

import java.util.Scanner;
import java.util.Random;
public class GuessingGame
{
   public static void main (String [] args)
   {


  //Variables
  Random randomNumber = new Random();
  Scanner kbd = new Scanner(System.in);
  int computerValue = randomNumber.nextInt(100);
  int numberOfTries = 0;
  int success = 0;
  int guess = 0;


  //Logic and While Loop



   while (success ==0)
     {
        System.out.println("please enter an integer betwen 1 and 100 inclusive: ");
        guess = kbd.nextInt();
        numberOfTries++;

           if (guess < 1 || guess > 100){
              System.out.println("Invalid input");
           }

           else if (guess == computerValue){
              success++;
              System.out.println("Congratulations you won! Your numbers of tries was: " + numberOfTries + " and the number was: " + computerValue);

           }
           else if (guess < computerValue){
              System.out.println("Your guess is too low!");
           }
           else if (guess > computerValue){
              System.out.println("Your guess is too high!");
    }
    }



   }
}

采纳答案by Pol

Try this:

尝试这个:

while(true) {
    computerValue = randomNumber.nextInt(100);
    numberOfTries = 0;
    while (true) {
        System.out.println("please enter an integer betwen 1 and 100 inclusive: ");
        guess = kbd.nextInt();
        numberOfTries++;

        if (guess < 1 || guess > 100) System.out.println("Invalid input");
        else if (guess == computerValue) {
            System.out.println("Congratulations you won! Your numbers of tries was: " + numberOfTries + " and the number was: " + computerValue);
            // leave the first loop
            break;
        }
        else if (guess < computerValue) System.out.println("Your guess is too low!");
        else if (guess > computerValue) System.out.println("Your guess is too high!");
    }

    System.out.println("Do you want to play again? (1:Yes/2:No)");
    // if input is not yes leave second loop
    if(kbd.nextInt() != 1) break;
}

回答by James

Prompt the user if he wants to play again at

提示用户是否想再次玩

else if (guess == computerValue) {
    success++;
    System.out.println("Congratulations you won! Your numbers of tries was: " + numberOfTries + " and the number was: " + computerValue);
}

If the user inputs yes success--;

如果用户输入 yes success--;

回答by Josh Wein

If you want to use a do while loop this would work.

如果您想使用 do while 循环,这将起作用。

import java.util.Scanner;
import java.util.Random;

public class GuessingGame {

public static void main(String[] args) {
    //Variables
    Random randomNumber = new Random();
    Scanner kbd = new Scanner(System.in);
    int computerValue;
    int numberOfTries = 0;
    int success = 0;
    int guess;
    boolean playAgain;
    //Logic and While Loop
    do {
        computerValue = randomNumber.nextInt(100);
        guess = 0;
        playAgain = false;
        while (guess != computerValue) {
            System.out.println("Please enter an integer betwen 1 and 100 inclusive: ");
            guess = kbd.nextInt();
            numberOfTries++;
            if (guess < 1 || guess > 100) {
                System.out.println("Invalid input");
            } else if (guess < computerValue) {
                System.out.println("Your guess is too low!");
            } else if (guess > computerValue) {
                System.out.println("Your guess is too high!");
            }
        }
        success++;
        System.out.println("Congratulations you won! Your numbers of tries was: " + numberOfTries + " and the number was: " + computerValue);
        System.out.println("Would you like to play again?");
        switch (kbd.next()) {
            case "yes":
                playAgain = true;
                break;
            default:
                break;
        }
    } while (playAgain);
    System.out.println("Goodbye");
}
}