当前位置:网站首页>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;
边栏推荐
- Returns a zero-length array or empty collection, do not return null
- 性能优化:记一次树的搜索接口优化思路
- OSPFv3的基本配置
- rj45对接头千兆(百兆以太网接口定义)
- leetcode 665. Non-decreasing Array
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
- STM32 full series development firmware installation guide under Arduino framework
- 利用反射实现一个管理对象信息的简单框架
- 1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
- 移动web开发02
猜你喜欢
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
中文编码的设置与action方法的返回值
Structure of the actual combat battalion module eight operations
Chinese encoding Settings and action methods return values
Arduino框架下STM32全系列开发固件安装指南
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
Memblaze released the first enterprise-grade SSD based on long-lasting particles. What is the new value behind it?
Short-circuit characteristics and protection of SiC MOSFETs
OSPFv3的基本配置
随机推荐
【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
Carbon教程之 基本语法入门大全 (教程)
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
sqlite3 simple operation
AI 自动写代码插件 Copilot(副驾驶员)
NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
返回一个零长度的数组或者空的集合,不要返回null
Douyin fetches video list based on keywords API
linux查看redis版本命令(linux查看mysql版本号)
How programmers learn open source projects, this article tells you
手把手教你学会部署Nestjs项目
如何才能真正的提高自己,成为一名出色的架构师?
What's wrong with the sql syntax in my sql
每月一书(202207):《Swift编程权威指南》
Basic configuration of OSPFv3
Socket Review and I/0 Model
MATLAB program design and application 2.4 Common internal functions of MATLAB
架构实战营模块 8 作业
Thymeleaf是什么?该如何使用。
程序员如何学习开源项目,这篇文章告诉你