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 guess numbers by Java

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

Share

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

This article will explain in detail how to guess numbers in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Guess the number

Title:

The program automatically generates a number between 1 and 100, and the utility implementation guesses what the number is? When guessing wrong, give corresponding hints according to different situations:

The guessed number is larger than the real number, which suggests that the guessed data is larger.

The guessed number is smaller than the real number, which suggests that the guessed number is smaller.

The guessed number is equal to the real number, which means congratulations on your guess.

Analysis:

Generate the number using a random number (range 0-100)

Repeat the guessing process using a loop

Use the keyboard to input guessing numbers

Compare the guessed number with the generated number

End the cycle after a good guess

Practice:

The code is as follows (example):

Public class Demo2 {public static void main (String [] args) {/ / do not like ginger, not jealous / / use random numbers to generate the number (range 0-100); / / need guide package import java.util.Random; Random r=new Random (); int num=r.nextInt (100) + 1; / / use loop to guess numbers repeatedly While (true) {/ / use the keyboard to enter guessed numbers; Scanner sc = new Scanner (System.in); System.out.println ("Please enter your guessed numbers:"); int s = sc.nextInt (); / / compare the guessed numbers with the generated numbers If (s > num) {System.out.println ("guess a big number!") ;} else if (s

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