做猜测游戏的循环

时间:2015-11-12 00:11:10

标签: java do-while

我正在编写一个简单的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!");
    }
    }



   }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

提示用户是否想再次播放

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

如果用户输入了“success--;

答案 2 :(得分:0)

如果你想使用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");
}
}