闲来无事,学到了Rand函数的用法,于是想到实现一个控制台抽奖的程序

程序功能比较简单,输入一个数值作为随机数的范围,为[1,range],通过不断按回车产生随机数,停止回车时固定。通过system(“cls”)实现清除之前的数据。输入任意其他字符+回车可以更换随机数范围。Ctrl+Z可停止程序运行。


#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    int range;
    while(cout << "输入随机数的范围 : ", cin >> range)
    {
        int ch;
        srand(time(NULL));
        while(ch = cin.get())
        {
            system("cls");
            if(ch == '\n')
                cout << "随机数 : " << (int)((rand()*1.0/RAND_MAX)*range) + 1 << endl;
            else if(ch == -1)
                return 0;
            else
                break;
        }
    }
    return 0;
}

C++ 控制台实现产生抽奖随机数C++ 控制台实现产生抽奖随机数C++ 控制台实现产生抽奖随机数




相关文章:

  • 2022-12-23
  • 2021-12-16
  • 2021-10-01
  • 2022-12-23
  • 2021-12-16
猜你喜欢
  • 2021-09-30
  • 2022-01-15
  • 2022-12-23
  • 2021-04-19
  • 2022-01-06
  • 2021-08-16
  • 2022-02-25
相关资源
相似解决方案