<> Figure guessing game
The system automatically generates a random integer (1-100), The user then enters a guessed number . If the number entered is smaller than the random number , Tips " low Yes ", If the number entered is larger than the random number ,
Tips " High " , If the number entered is equal to the random number , Then prompt " You guessed right

<> Organize ideas

1. When we play games , Both start and exit the game
2. secondly , It generates a random number , If it is a fixed value , What's the point ?
3. also , We need to enter numbers , Judge and guess the size of the number according to its feedback
4. But we can't judge success once , So we need a loop input
5. last , After guessing the number correctly , We need to exit game mode , Return to the menu interface , Let users choose to play or not to play , You can't say let others play all the time , Can't you get out . That's a bad game, isn't it

<> Now we officially start making
import java.util.Random; import java.util.Scanner; public class LogicControl {
// be careful Self determined function menu and game Location of , No need to put it in main Front of method , because java in It's impossible to declare a function , You can call it directly if you want to use it public
static void menu(){// Game menu selection interface System.out.println("*****************"); System.
out.println("**** 1.Play ****"); System.out.println("**** 0.Exit ****"); System.
out.println("*****************"); } public static void game(){// Game running program Scanner
scanner= new Scanner(System.in);// Generate a random value for , Prepare Random random = new Random();
// The default random seed is system time , Tips Put numbers in parentheses , It generates a specified number , So it's best not to paint the snake int toGuess = random.nextInt(100);
// generate 100 Number within while(true){// Dead cycle , Don't guess right, don't quit System.out.println(" Please enter the number you guessed "); int
guess= scanner.nextInt(); if(guess < toGuess){ System.out.println(" Guess it's small "); }else
if(guess > toGuess){ System.out.println(" Guess big "); }else{// guess == toGuess System
.out.println(" You guessed right "); break;// You guessed right , Have the right to quit , Of course, you take the game card away , Forced exit from the game , What can I do with you ? }// chart 55 } }
public static void main(String[] args) {// first , subject Classes and methods You have to write it first Scanner scanner = new
Scanner(System.in);// Get ready for us to enter data int input = 0;// Input variable initialization do{ menu();//
Players enter the game , It must have been the game interface at first System.out.println(" Please select :");// According to our game interface options , Determines the value entered input =
scanner.nextInt();// Input variable update switch(input){// use switch sentence conduct , Judgment of input data case 1:{
// If it is 1, namely play play a game game();// use game Custom function , chart 54 break;// game over , Exit the game , Return to the main game interface . chart 56 }
case 0:{// If it is 0, Exit program , Stop playing ! System.out.println(" Exit the game "); break;// chart 57 } default:{
// For example, use the data entered by the user , Not the data I specified , Then we need to feed back user information , Let the user reselect System.out.println(" Input error , Please re-enter ");// chart
53 break;// be careful break Yes switch And use ,break Now is the end switch sentence , Not a circular statement // This is
break Special usage of } } }while (input>0);// Conditional judgment , If input 0 And negative numbers , After executing the above procedure , Exit loop ( This is do while
Characteristics of , Do it first ), End the game // If you enter a positive number , Then the cycle continues , But if the positive number is not 1 You can't play games , The system will inform you of the input error , Please reselect } }
<> chart 53

<> chart 54

<> chart 55

<> chart 56

# chart 57