当前位置:网站首页>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;
边栏推荐
- leetcode 665. Non-decreasing Array
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
- spark reports an error OutOfMemory "recommended collection"
- Cache and Database Consistency Solutions
- 有一说一,外包公司到底值不值得去?
- 架构实战营模块八作业
- ReentrantLock原理(未完待续)
- 【AcWing】第 62 场周赛 【2022.07.30】
- C# 之 扑克游戏 -- 21点规则介绍和代码实现
- Write a database document management tool based on WPF repeating the wheel (1)
猜你喜欢
ReentrantLock原理(未完待续)
基于STM32 环形队列来实现串口接收数据
AI automatic code writing plugin Copilot (co-pilot)
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
手把手教你学会部署Nestjs项目
A shortcut to search for specific character content in idea
统计UTF-8字符串中的字符函数
如何才能真正的提高自己,成为一名出色的架构师?
请问我的这段sql中sql语法哪里出了错
随机推荐
What is Thymeleaf?How to use.
Write a database document management tool based on WPF repeating the wheel (1)
Apache EventMesh distributed event-driven multi-runtime
Count characters in UTF-8 string function
1161. 最大层内元素和 : 层序遍历运用题
【AcWing】第 62 场周赛 【2022.07.30】
MySQL - multi-table query
spark reports an error OutOfMemory "recommended collection"
Three. Introduction to js
Socket回顾与I/0模型
Performance optimization: remember a tree search interface optimization idea
leetcode 665. Non-decreasing Array
广汽本田安全体验营:“危险”是最好的老师
Cache and Database Consistency Solutions
AI automatic code writing plugin Copilot (co-pilot)
Teach you how to deploy Nestjs projects
rj45 to the connector Gigabit (Fast Ethernet interface definition)
uni-app中的renderjs使用
MySQL---aggregate function
The old music player WinAmp released version 5.9 RC1: migrated to VS 2019, completely rebuilt, compatible with Win11