Java guessing game (generation)

Here is an implementation with random numbers and console output

1. Code block:

package guessing game;

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

public class Game {     public static void main(String[] args) {         //Call the static game method to play the game         game();     }



    public static void game() {         System.out.println("Welcome to the guessing game!");         Random r=new Random();         //Get a random number from 1-100         int i=r.nextInt(100 )+1;         //Define a value x that has been guessed a few times         int x=0;         //Perform an infinite loop to guess the number         while(true) {             x++;//Increase the number by one for each guess









            

//Make a judgment on the number of times. If you have guessed 5 times before, you can no longer guess. This 6 is because when the infinite loop starts, the number of times will increase by one, so it is equivalent to you can't play after playing 5 times. up
            

if(x<6) {                 //Output in the console, the number to guess         Scanner e=new Scanner(System.in);         System.out.println("Please enter the number you want to guess:");         int index =e.nextInt();         //This judgment is to judge the size of the number you guessed and the random number of the system         if(index<i) {             System.out.println("The number you guessed"+index+ "too small");         }else if(index>i) {             System.out.println("The number you guessed"+index+"too big");         }else {             System.out.println("Congratulations, you won Big prize!");             break;//If you guessed right, you have to exit the loop         }         }             else {                 System.out.println("Unfortunately, the number of guesses has been used up this time");                 break;//If the number of guesses exceeds 5 times, it is also necessary to exit the loop



        

        













            }}
    }

2. Design ideas: First of all, if you want to guess the number, the system must give a random number, and then the user will guess. If we want to interact with the computer, we must use the console output of the Scanner class to realize the interaction. ;Secondly, you need to judge whether you have guessed correctly. If you have not guessed correctly, you should also prompt whether the guess is too big or too small to determine the direction to continue guessing. Here, choose the if-else if-else statement; If the guess is correct, you have to continue to guess. Here, a loop is used, because it is not sure how many times to guess, so an infinite loop of while is used.

It should have ended here, but in order to increase the fun and playability (to avoid guessing all the time, you can only guess right), and then set a parameter x. In this game, record the number of guesses you have made. And in the infinite loop while, add an if-else statement, so that the number of guesses is less than 6 times, that is, a maximum of five guesses. If it exceeds, the loop will automatically end, and the game will fail to guess the number. If it is guessed within 5 times , it succeeds.

 

 

Guess you like

Origin blog.csdn.net/m0_62780474/article/details/123950827