C++入门教程(4)猜数字游戏(rand)

上节说到一个猜数字的游戏,我们首先分析下这个小游戏的流程

1.首先系统产生一个需要猜的数字N(1<N<100)

2.输入一个数字,系统给出是大了还是小了,进一步判断数字的范围

3.重复输入数字,直到猜中数字

#include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    //std::cout << "Hello, World!\n";
    
    //rand() 可以产生一个随机数 %取余 即可得到一个0-100的随机数
    int number = rand() %100 ;
    
    //input 用于获取玩家输入的数字
    int input = 0;
    //maxNum 用于保存当前能判断的最大值
    int maxNum = 100;
    //minNum 用于保存当前能判断的最小值
    int minNum = 1;
    
    cout << "请输入数字(1-100):";
    while(input != number)
    {
        cin >> input;
        if(input > number)
        {
            if (input < maxNum) {
                maxNum = input;
            }
            cout << "大了,请输入数字("<< minNum << "-"<< maxNum << ") \n" ;
        }
        else if(input < number)
        {
            if (input > minNum) {
                minNum = input;
            }
            cout << "小了,请输入数字("<< minNum << "-"<< maxNum << ") \n" ;
        }
        else{
            cout << "恭喜你,猜对了,数字是:" << number <<endl;
        }
    }
    
    return 0;
}

        运行,不断根据提示最终我们得到了最终的答案,7

请输入数字(1-100):50
大了,请输入数字(1-50) 
25
大了,请输入数字(1-25) 
12
大了,请输入数字(1-12) 
6
小了,请输入数字(6-12) 
7
恭喜你,猜对了,数字是:7
Program ended with exit code: 0

        真好玩,再玩一次,等一下。。。嗯? 怎么。还是7

        不是说好了随机产生一个值么?!为什么一直是7?

        其实rand()是伪随机,每次产生包括下一次产生的数都是固定的,我们连续输出10次

    for(int i = 0; i< 10 ;i++)
    {
        cout<< rand()%100<< "  ";
    }

        这个结果永远都是

7  49  73  58  30  72  44  78  23  9

这个时候,需要引入一个随机的种子,这个种子可以是任意值,通常采用时间,我们修改一下代码,让每次产生的随机值都不同


#include <iostream>
#include <time.h>
using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    //std::cout << "Hello, World!\n";
    
    //如果随机种子是固定的值 则每次运行的结果还是一样的
    //srand(12);
    
    
    //随机种子为当前时间
    srand(time(NULL));

//    //测试随机产生10个随机数
//    for(int i = 0; i< 10 ;i++)
//    {
//        cout<< rand()%100<< "  ";
//    }
    
    //rand() 可以产生一个随机数 %取余 即可得到一个0-100的随机数
    int number = rand() %100 ;
    
    //猜之前查看是否每次都是随机的
    //cout << number ;
    
    //input 用于获取玩家输入的数字
    int input = 0;
    //maxNum 用于保存当前能判断的最大值
    int maxNum = 100;
    //minNum 用于保存当前能判断的最小值
    int minNum = 1;
    
    cout << "请输入数字(1-100):";
    while(input != number)
    {
        cin >> input;
        if(input > number)
        {
            if (input < maxNum) {
                maxNum = input;
            }
            cout << "大了,请输入数字("<< minNum << "-"<< maxNum << ") \n" ;
        }
        else if(input < number)
        {
            if (input > minNum) {
                minNum = input;
            }
            cout << "小了,请输入数字("<< minNum << "-"<< maxNum << ") \n" ;
        }
        else{
            cout << "恭喜你,猜对了,数字是:" << number <<endl;
        }
    }
    
    return 0;
}

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, If they enter incorrectly, the program should set the bet to their balance and tell them this. Once a bet is entered, they must pick a number between 1 and 10 (you do not need to errorcheck or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10). If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars( 50/5 * (7-5) = 20 ). However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, (40/5 * (10-1) = 72 > 50 )then they should lose 50. After each turn, the program should display that turn's winnings (or loss amount) and the new balance,and then repeat the game until the user either quits or has no money left. Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly (except for your name). An appropriate welcome message with your name in it should be shown at the start of the program. Assignment 1 : Guessing Game Written by yourname Please enter your bet (up to $100.00): $50 Guess a number between 1 and 10: 5 Correct! You win $50.00 Your new balance is $150.00 Please enter your bet (up to $150.00): $200 Your bet is $150.00 Guess a number between 1 and 10: 6 Wrong! The computer chose: 4 You lose $60.00 Your new balance is $90.00 Please enter your bet (up to $90.00): $80 Guess a number between 1 and 10: 1 Wrong! The computer chose: 7 You lose $90.00 Thank you for playing! Press any key to continue . . . Another Sample: CP1200 Guessing Game Written by Lindsay Ward Please enter your bet (up to $100.00): $-20 Your bet is $100.00 Guess a number between 1 and 10: 5 Wrong! The computer chose: 2 You lose $60.00 Your new balance is $40.00 Please enter your bet (up to $40.00): $10.50 Guess a number between 1 and 10: 12 Wrong! The computer chose: 2 You lose $21.00 Your new balance is $19.00 Please enter your bet (up to $19.00): $0 Thank you for playing! Press any key to continue . . . srand( static_cast<int>(time(0))) ; 初始化 computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout << fixed << setprecision(2) ; 控制输出显示2小数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱我呦呦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值