<> preface

This article mainly shares one Java Figure guessing games .

<> one , Guess what the number game is ?

The number guessing game is a random number generated by the program , By prompting the user to constantly enter the number he guesses , Determine whether it is the same as the random number , If it's the same, you're right , Otherwise, you didn't guess right .

<> two , Title and implementation steps of case project

<>1. subject

<> Randomly generated number n(1-100), Wait for the user to enter guess data , According to the user's input, the output is larger , Guess it's small , You guessed right , If the user guesses right, the game ends .

<>2. Problem solving ideas

* First, the code generates random numbers
* Prompt the user to enter the number he guessed
* Judge whether they are equal through conditional judgment , If equal , You guessed right , Or you'll continue to guess the numbers
* External nested loop can be used for condition judgment , In this way, you can achieve the effect of prompt all the time , If you guessed right, you can execute it break Jump out of the loop
* The code can be optimized at the end , It can realize the code such as rating according to the number of user guesses
<>3. code implementation
import java.util.Scanner; import java.util.Random; public class ForGuessTest {
public static void main(String[] args) { // 1. Random generation 1 ~ 100 And record with variables Random ra
= new Random(); int temp = ra.nextInt(100) + 1; // yes 100 Surplus , The result is 0 ~ 99, Add one as 1 ~ 100
// System.out.println("temp = " + temp); // 5. Declare a int Type to count the number of user guesses int count =
0; // 4. Build an endless loop and guess numbers many times for(;;) { // 2. Prompt user for input 1 ~ 100 And record with variables System.out.
println(" Please enter 1 ~ 100 Integer between guesses :"); Scanner sc = new Scanner(System.in); int num =
sc.nextInt(); count++; // 3. Compare the size and give the corresponding prompt if(num > temp) { System.out.println(
" Guess big , A little smaller "); } else if(num < temp) { System.out.println(" Guess it's small , Please make it bigger "); }else {
System.out.println(" Congratulations , You guessed right "); break; } } if(1 == count) { System.out.println(
"=================NB================"); } else if(count <= 6) { System.out.
println("=================Good=============="); } else { System.out.println(
"=================Practice=========="); } } }
<>4. Operation results
> java ForGuessTest Please enter 1 ~ 100 Integer between guesses : 50 Guess it's small , Please make it bigger Please enter 1 ~ 100 Integer between guesses : 75
Guess it's small , Please make it bigger Please enter 1 ~ 100 Integer between guesses : 90 Guess big , A little smaller Please enter 1 ~ 100 Integer between guesses : 80 Congratulations , You guessed right
<> summary

Understanding the libraries that need to be imported can greatly improve the efficiency of code development , At the same time, if you have a clear thinking before solving the problem , This requirement can be realized quickly and well , come on. , record Java Learning bit by bit .

Technology