简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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