Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize number guessing Mini Game

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use Java to realize guessing number Mini games," in daily operation, I believe many people have doubts on how to use Java to realize guessing number Mini games, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "how to use Java to realize guessing number Mini games" doubts helpful! Next, please follow the small series to learn together!

guess the number game

The system automatically generates a random integer (1-100), and then the user enters a guess number. if the number entered is smaller than the random number, the prompt "low" will be displayed.

If the number entered is larger than the random number, prompt "high" , if the number entered is equal to the random number, prompt "guess right"

organize one's thoughts

1. When we play games, we all start games and quit games.

2. Second, it generates a random number. If it's fixed, what's the point?

3. Furthermore, we have to input numbers, and judge and guess the size of the number according to its feedback.

4. But we can't say it once, so we need a loop input.

5. Finally, after guessing the number correctly, we need to exit the game mode and go back to the menu interface to let the user choose whether to play or not. You can't say that let others play all the time and can't quit. So this game sucks, huh?

import java.util.Random;import java.util.Scanner;public class LogicControl //Note the position of custom function menu and game, do not need to be placed in front of the main method, because java is no function declaration, to use the direct call 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 Runner Scanner = new Scanner(System.in);//Prepare to generate a random value Random random = new Random();//default random seed is system time, prompt in parentheses with a number, it generates a specified number, so it is best not to add unnecessary int toGuess = random.nextInt(100);//Generate numbers up to 100 while(true){//endless loop, do not guess right do not quit System.out.println("Enter your guess"); int guess = scanner.nextInt(); if(guess

< toGuess){ System.out.println("猜小了"); }else if(guess >

toGuess){ System.out.println(""); }else{// guess == toGuess System.out.println(""); break;//guess correctly, you have the right to quit, of course, you take away the game card, forced to quit the game, what can I do with you? }//Figure 55 } } public static void main(String[] args) {//First, write the body class and method first Scanner = new Scanner(System.in);//Get ready for our input data int input = 0;//Input variable initialization do{ menu();//Players enter the game, the game interface must be at the beginning System.out.println("Please select: ");//Depending on our game interface options, determine the value entered input = scanner.nextInt();//input variable update switch(input){//Use switch statement to judge input data Case 1:{//if it is 1, it is play game();//use game custom function, Figure 54 break;//game ends, exit the game, return to the main game interface. Fig. 56 } case 0:{//If 0, quit the program and stop playing! System.out.println("exit game"); break;//Figure 57 } default:{//If the data entered by the user is not the data specified by me, we need to feed back the user information so that the user can make a new selection. System.out.println("Input error, please re-enter");//Figure 53 break;//Note that break can be switched and used, break is now a termination switch statement, not a loop statement //This is a special use of break } } }while (input>0);//conditional judgment, if you enter 0 and negative, after executing the above program, exit the loop (this is the characteristic of do while, do it first), end the game //If you enter a positive number, the loop continues, but if the positive number is not 1, you cannot play the game. The system will inform you that the input is wrong. Please select again. }}

Fig. 1

Fig. 2

Fig. 3

Fig. 4

Fig. 5

At this point, about "how to use Java to realize guessing number Mini games" learning is over, I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report