seo查询工具网站,西部数码WordPress开启伪静态,一般网站设计多大宽度,衡阳衡阳县网站建设文章目录 6.34题目代码运行截图6.35题目代码运行截图 6.34题目
猜数字游戏)编写一个程序#xff0c;可以玩“猜数字”的游戏。具体描述如下:程序在1~1000之间的整数中随机选择需要被猜的数#xff0c;然后显示:
代码
#include iostream
#include cstdlib… 文章目录 6.34题目代码运行截图6.35题目代码运行截图 6.34题目
猜数字游戏)编写一个程序可以玩“猜数字”的游戏。具体描述如下:程序在1~1000之间的整数中随机选择需要被猜的数然后显示:
代码
#include iostream
#include cstdlib
#include ctimeusing namespace std;int main()
{srand(static_castunsigned int(time(0)));while (1){int guessNumber rand() % 1000 1;char YesOrNo;cout I have a number between 1 and 1000.\n;cout Can you guess my number ?\nPlease type your first guess.\n ;while (1){int guess;cin guess;if (guess guessNumber){cout Too high.Try again.\n;}else if (guess guessNumber){cout Too low.Try again.\n;}else{cout Excellent! You guessed the number!\n;cout Woule you like to play again (y or n)?\n;cin YesOrNo;break;}}if (YesOrNo n)break;}return 0;
}
运行截图 6.35题目
(猜数字游戏的修改)修改练习题6.34 中的程序统计玩家猜想的次数。如果次数没有超过10次打印“Either you know the secret or you got lucky!”。如果玩家10 次才猜中,打印出“Ahah! You knowthe secret!”。如果玩家超过10 次才猜中打印“You should be able to do better!”。为什么猜测次数不应超过 10 次呢?因为在每次“好的猜想”过程中玩家应该能够排除一半的数。现在说明了为什么任何1~1000之间的数字能够不超过10次就被猜中。
代码
#include iostream
#include cstdlib
#include ctimeusing namespace std;int main()
{srand(static_castunsigned int(time(0)));while (1){int count 0; // 用于计算猜测次数int guessNumber rand() % 1000 1;char YesOrNo;cout I have a number between 1 and 1000.\n;cout Can you guess my number ?\nPlease type your first guess.\n ;while (1){int guess;cin guess;count; // 猜测次数加一if (guess guessNumber){cout Too high.Try again.\n;}else if (guess guessNumber){cout Too low.Try again.\n;}else{cout Excellent! You guessed the number!\n;cout Woule you like to play again (y or n)?\n;cin YesOrNo;break;}}if (count 10) // 添加的代码输出cout Either you know the secret or you got lucky!;else if (count 10)cout Ahah! You know the secret!;elsecout You shoule be able to do better!;if (YesOrNo n){break;}}return 0;
}
运行截图