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 simple number guessing Mini Game

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Java to achieve simple number guessing Mini Game", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Java to achieve simple number guessing Mini Game" bar!

Realization idea

An integer of 1 to 100 is randomly generated by the computer. The user guesses the number generated by the computer and inputs the number, and when the input number is output at the same time as the number generated by the computer, congratulations, you guessed right. When the input number is less than the computer-generated digital output, it is too small, when the input number is larger than the computer-generated digital output, it is too large.

You can use the for loop to set the number of guesses of the game to increase the playability of the code

The code implements import java.util.Random;import java.util.Scanner;public class User {public static void main (String [] args) {{Random ran = new Random (); / / instantiates the random object System.out.println ("Welcome to the guessing game, have a good time"); int b = ran.nextInt + 1 / / randomly generate an integer as the guessed answer int i; for (I = 0; I

< 10; i++) {//给游戏设定一个次数,最多输入10次 System.out.println("请输入一个整数:"); Scanner a = new Scanner(System.in);//实例化一个输入流对象 if (a.hasNextInt()) {//判断输入的数是否为int类型 var c = a.nextInt();//将输入的值赋给c if (c < b) {//判断c是否小于答案,如果是就输出下面这段话 System.out.printf("太小了,还剩%2d次机会", 9 - i); System.out.println(); } else if (c >

B) {/ / determine whether c is greater than the answer, and if so, output the following words System.out.printf ("too big, there are% 2d opportunities left", 9-I); System.out.println () } else {/ / otherwise equals the answer System.out.println ("Congratulations, you guessed right"); System.out.println ("Game over, win"); break }} else {/ / if you do not enter an int type, run the following code if ("don't want to play" .equals (a.next () {/ / to determine whether you do not want to play these words, and if so, execute the following sentence System.out.println ("end the game" Stop playing ") Break;} else {/ / otherwise output the following words System.out.println ("input error"); iMel- / / iMur1 this input does not count the number of times} if (I = = 10) {/ / determine whether the input number is 10 times, and if it is 10 times, output the following words System.out.println ("Game over, failure");}

Welcome to the guessing game. Have a good time.

Please enter an integer:

fifty

It's too young. There are nine chances left.

Please enter an integer:

sixty

It's too big. There are eight chances left.

Please enter an integer:

fifty-five

It's too big. There are seven chances left.

Please enter an integer:

fifty-three

You guessed it right.

The game is over. Win.

Thank you for your reading, the above is the content of "how to use Java to achieve simple number guessing Mini Game". After the study of this article, I believe you have a deeper understanding of how to use Java to achieve simple number guessing Mini Game, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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