C++大学教程(第九版)6.34猜数字游戏 6.35 修改的猜数字游戏

6.34题目

猜数字游戏)编写一个程序,可以玩“猜数字”的游戏。具体描述如下:程序在1~1000之间的整数中随机选择需要被猜的数,然后显示:
在这里插入图片描述

代码

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(static_cast<unsigned 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 <ctime>

using namespace std;

int main()
{
    srand(static_cast<unsigned 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!";
        else
            cout << "You shoule be able to do better!";

        if (YesOrNo == 'n')
        {
            break;
        }
    }
    return 0;
}

运行截图

在这里插入图片描述

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值