In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java guess how to write the number of Mini games code, many novices are not very clear about this, in order to help you solve this problem, the following small series will be explained in detail for everyone, there are people who need this to learn, I hope you can gain something.
▲ Rules of the game:
The system randomly generates a number in the range of 0--100, the user inputs the number, judges whether the number is greater than, less than, or equal to the randomly generated number, and exits the program when it is equal.
▲ Implementation ideas:
The first thing we have to solve is random numbers, which are meaningless if we set them artificially. Java provides a Random class in the java.util package that implements system input. Once the random number is solved, we are halfway there. Let's see how the code executes.
import java.util.Random;import java.util.Scanner;public class Game {。 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int a = random.nextInt(100); System.out.println("You can start guessing"); while (scanner.hasNextInt()) {//loop read int a1 = scanner.nextInt(); if (a1 > a) { System.out.println(""); } else if (a1
< a) { System.out.println("小了"); } else { System.out.println("对了!!"); break; } } }}实验结果:To check the correctness, you can print out the random number a before you start guessing. Try this game.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.