简体   繁体   English

做 while 循环猜猜游戏

[英]Do while loop for guessing game

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.我正在编写一个简单的 java 猜谜游戏,该游戏应该随机选择 1 到 100 之间的一个数字,然后在您选择正确的数字后,它会询问您是否想再玩一次。 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.我知道我应该能够使用 Do while 循环来询问用户是否想再次播放,但是每次我尝试都无法使其工作。 This is my fully functional program without the do while loop.这是我没有 do while 循环的全功能程序。 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.如果缩进不是 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!");
    }
    }



   }
}

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;
}

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--;

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");
}
}

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

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