电脑随机产生1000—9999之间的数字(包括1000、9999);
提示:用rand函数,该函数在stdlib.h库中,并保证多次运行产生不同的随机数
游戏以百分制形式,初始分为100,最后得分最多为赢家。
判断输入数字比产生的数字大还是小 ——扣当前分数的1/10,,
#include
#include
int sort = 100;
int main()
{
int in = 0;
int r = 0;
while (1)
{
r = rand();
while (1000 <= r && r <= 9999)
{
printf("please enter data:");
scanf("%d", &in);
if (in > r || in < r) //如果大或小,减去当前分数的1/10
sort -= sort / 10;
printf("当前分数%d\n", sort);
}
}
return 0;
}
意思是猜n次,每次猜错都是减少1/10分,直到猜到了那个数啊!一次猜到就100...是么?
不想玩了!给你自己玩!
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int n,m=0;
float score = 100;
int Random=20;//定义随机数
bool f = true;
srand(time(0));
char t;
cout << "你要玩游戏吗?请输入y/n\n";
while (f)
{
cin >> t;
if (t == 'y' || t == 'Y' || t == 'N' || t == 'n')f = false;
else cout << "请重新输入"<<endl;
}
system("cls");
while (t == 'y'||t=='Y' )
{
int max = 9999, min = 1000;
while (Random < 1000 || Random>9999)//随机出来的值范围是1到10000,排除掉1000以下的
{
for (int i = 0; i < 10; i++)
{
Random = rand() % 10000 + 1;//随机数出来,循环十次是因为时间间隔太短
cout << "第" << i + 1 << "个输出随机数的值:" << Random << "\t\n";
}
}system("pause");
cout << "亲,请输入你猜的数字(注意范围在1000到9999):" << endl;
f = true;
while (f)
{
system("cls");
cout << "亲,请输入你猜的数字,当前范围是" << min << "到" << max << ":\n";
cin >> n;
if (Random > n)
{
min = n;
cout << "你猜的数是:" << n << "。你猜的数小了,请重新输入。";
m++;
score = score - score / 10;
}
else if (Random < n)
{
max = n;
cout << "你输入的数:" << n << "。你猜的数大了";
cout << "请重新输入:";
m++;
score = score - score / 10;
}
else
{
m++;
system("color fc&&cls");
cout << "===================恭喜你!你猜的数对了=====================" << endl;
cout << "-------------------你的分数是" << score<<"---------------------"<<endl;
cout<< "-------------------你猜的次数是:" << m << "---------------------" << endl;
system("pause");
f = false;
}
}
system("color 07");
cout << "你还要继续吗?Y/N" << endl;
Random = 20;
cin >> t;
}
cout << "谢谢大家的参与:" << endl;
return 0;
}