当前位置:网站首页>每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
2022-07-31 16:34:00 【北柠陌语】
题目: 随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
解题关键:猜测次数的控制以及是第几次猜中的
思路:1.生成随机数
2.提示可以猜测的次数
3.while判断猜测的次数
4.判断猜测机会是否用完
过程: 接下来我们根据我们的解题思路来一步步写代码
1.生成随机数
Scanner sc = new Scanner(System.in);
int num = (int)(Math.random()*99 +1);
2.提示可以猜测的次数
System.out.println("您猜测的次数不能超过7次");
int i = 1;//开始是第一次
3.while判断猜测的次数
while(i < 8){
System.out.println("请猜测一个1-100之间的整数");
int guessNum = sc.nextInt();
System.out.println("您输入的数字为:" + guessNum);
if(guessNum < num){
System.out.println("您输入的数字小了");
}else if( guessNum > num){
System.out.println("您输入的数字大了");
}else{
System.out.println("恭喜您,您猜中了,您是在第"+i+"次猜中的");
break;
}
i++;
4.判断猜测机会是否用完
if(i == 8){
System.out.println("很遗憾,您的7次机会用完了,您没有猜中");
System.out.println("随机数为:" + num);
}
}
完整结果如下:

为了方便大家使用,下面附上源码:
//1.生成随机数
Scanner sc = new Scanner(System.in);
int num = (int)(Math.random()*99 +1);
//System.out.println("随机生成的数字为:" + num);
//2.提示可以猜测的次数
System.out.println("您猜测的次数不能超过7次");
int i = 1;//开始是第一次
//3.while判断猜测的次数
while(i < 8){
System.out.println("请猜测一个1-100之间的整数");
int guessNum = sc.nextInt();
System.out.println("您输入的数字为:" + guessNum);
if(guessNum < num){
System.out.println("您输入的数字小了");
}else if( guessNum > num){
System.out.println("您输入的数字大了");
}else{
System.out.println("恭喜您,您猜中了,您是在第"+i+"次猜中的");
break;
}
i++;
//4.判断猜测机会是否用完
if(i == 8){
System.out.println("很遗憾,您的7次机会用完了,您没有猜中");
System.out.println("随机数为:" + num);
}
}总结:
这道题目的关键在于猜测次数的判断(猜七次后没有猜中)以及是第几次猜中的
明日练习:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
大家可以自己写写,明天中午12点我准时发出我的写法哦,明天12点不见不散
一生朋友一生情,一生有你才会赢;千山万水总是情,点个关注行不行!

边栏推荐
猜你喜欢

After Effects tutorial, How to adjust overexposed snapshots in After Effects?

.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world

EF Core 2.2中将ORM框架生成的SQL语句输出到控制台

C language - function

Implementing DDD based on ABP

AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)

Qt practical cases (54) - using transparency QPixmap design pictures

2022年整理LeetCode最新刷题攻略分享(附中文详细题解)

基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀

Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
随机推荐
6-22 Vulnerability exploit - postgresql database password cracking
2022年必读的12本机器学习书籍推荐
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
Masterless replication system (1) - write DB when node fails
自动化测试—web自动化—selenium初识
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
动态规划(一)
研发过程中的文档管理与工具
Multi-datacenter operation and detection of concurrent writes
[TypeScript] OOP
字符指针赋值[通俗易懂]
2022年整理LeetCode最新刷题攻略分享(附中文详细题解)
无主复制系统(1)-节点故障时写DB
How Redis handles concurrent access
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
After Effects tutorial, How to adjust overexposed snapshots in After Effects?
[7.28] Code Source - [Fence Painting] [Appropriate Pairs (Data Enhanced Version)]
Golang 小数操作之判断几位小数点与四舍五入
go图书管理系统
Premiere Pro 2022 for (pr 2022)v22.5.0