当前位置:网站首页>Daily practice——Randomly generate an integer between 1-100 and see how many times you can guess.Requirements: The number of guesses cannot exceed 7 times, and after each guess, it will prompt "bigger"
Daily practice——Randomly generate an integer between 1-100 and see how many times you can guess.Requirements: The number of guesses cannot exceed 7 times, and after each guess, it will prompt "bigger"
2022-07-31 21:10:00 【Bei Ning Mo language】
Title: Randomly generate an integer between 1-100 and see how many times you can guess.Requirements: The number of guesses cannot exceed 7 times, and after each guess, it will prompt "bigger" or "smaller".
The key to solving the problem:The control of the number of guesses and the number of guesses
Ideas:1. Generate random numbers
2. Prompt the number of times you can guess
3.while judging the number of guesses
4. Judge whether the guessing opportunities are used up
Process: Next, we will write the code step by step according to our problem-solving ideas
1. Generate random numbers
Scanner sc = new Scanner(System.in);
int num = (int)(Math.random()*99 +1);
2. Prompt the number of guesses
System.out.println("You can't guess more than 7 times");
int i = 1;//Start isThe first time
3.while judging the number of guesses
while(i < 8){
System.out.println("Please guess an integer between 1-100");
int guessNum = sc.nextInt();
System.out.println("The number you entered is:" + guessNum);
if(guessNum < num){
System.out.println("The number you entered is too small");
}else if( guessNum > num){
System.out.println("The number you entered is too big");
}else{
System.out.println("Congratulations, you guessed correctly, you are in the "+i+"th guess");
break;
}
i++;
4. Determine whether the guessing opportunities are used up
if(i == 8){
System.out.println("Unfortunately, you ran out of 7 chances and you didn't guess correctly");
System.out.println("The random number is:"+ num);
The full results are as follows:

For your convenience, the source code is attached below:
//1. Generate random numbersScanner sc = new Scanner(System.in);int num = (int)(Math.random()*99 +1);//System.out.println("The randomly generated number is: " + num);//2. Prompt the number of times you can guessSystem.out.println("You can't guess more than 7 times");int i = 1;//Start is the first time//3.while judge the number of guesseswhile(i < 8){System.out.println("Please guess an integer between 1-100");int guessNum = sc.nextInt();System.out.println("The number you entered is: " + guessNum);if(guessNum < num){System.out.println("The number you entered is too small");}else if( guessNum > num){System.out.println("The number you entered is too big");}else{System.out.println("Congratulations, you guessed it right, you guessed it in the "+i+"th time");break;}i++;//4. Determine whether the guessing opportunities are used upif(i == 8){System.out.println("Unfortunately, you ran out of 7 chances and you didn't guess correctly");System.out.println("The random number is: " + num);}}Summary:
The key to this question is the judgment of the number of guesses (the guess is not successful after seven guesses) and the number of guesses
Tomorrow's practice:There are n integers, so that the previous numbers are moved back m positions in order, and the last m number becomes the mostThe number of m in front
You can write by yourself, I will send my writing on time at 12 noon tomorrow, see you at 12 tomorrow
A lifetime of friends is a lifetime of love, and you will only win if you have a lifetime;

边栏推荐
- 1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
- multithreaded lock
- MySQL---sort and pagination
- 【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
- 【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
- 使用 Flutter 和 Firebase 制作!计数器应用程序
- 老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
- 架构实战营模块八作业
- 利用反射实现一个管理对象信息的简单框架
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
猜你喜欢

Structure of the actual combat battalion module eight operations

手把手教你学会部署Nestjs项目

Socket回顾与I/0模型

第六章
![leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]](/img/91/284de3dcbb8d143d85775b314dd41c.png)
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]

Count characters in UTF-8 string function

GAC Honda Safety Experience Camp: "Danger" is the best teacher

Shell script quick start to actual combat -02

Realization of character makeup

Introduction to Audio Types and Encoding Formats in Unity
随机推荐
-xms -xmx(information value)
How programmers learn open source projects, this article tells you
Book of the Month (202207): The Definitive Guide to Swift Programming
请问我的这段sql中sql语法哪里出了错
使用 Flutter 和 Firebase 制作!计数器应用程序
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
sqlite3 simple operation
Bika LIMS open source LIMS set - use of SENAITE (detection process)
基于STM32 环形队列来实现串口接收数据
【AcWing】The 62nd Weekly Match 【2022.07.30】
Carbon教程之 基本语法入门大全 (教程)
Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization
Socket Review and I/0 Model
架构师04-应用服务间加密设计和实践
SiC MOSFET的短路特性及保护
multithreaded lock
MySQL - single function
Count characters in UTF-8 string function
Financial profitability and solvency indicators
Mobile web development 02