C++猜数字游戏

2024-11-22 16:38:51
推荐回答(4个)
回答1:

#include
#include
using namespace std;

int main()
{
srand((unsigned)time(NULL));

int num, temp, max=101, min=-1, n=0;

cout << "输入一个数:" << endl;
cin >> num;

temp = rand() % max;
n++;
cout << "电脑——>" << temp << endl;

int i = 0;

while(i != 3)
{
cout << "选择结果( 1代表<, 2代表>, 3代表= ):" << endl;
cin >> i;

switch (i)
{
case 1:
max = temp;
cout << "数大了!" << endl;
temp = rand() % max;
while (temp <= min)
{
temp = rand() % max;
}
n++;
cout << "电脑——>" << temp << endl;
break;
case 2:
min = temp;
cout << "数小了!" << endl;
temp = rand() % max;
while (temp <= min)
{
temp = rand() % max;
}
n++;
cout << "电脑——>" << temp << endl;
break;
case 3:
cout << "正确!" << endl;
break;
default:
break;
}
}

cout << "电脑总共猜了" << n << "次" << endl;

return 0;
}

回答2:

手动判断太慢,我改成自动判断
#include
#include
#include
#include
using namespace std;
int main()
{
srand((unsigned int)time(0));
int iInputNum;
int iRandomNum;
int i=0;
cout<<"输入一个0—10的整数"< do
{
cin>>iInputNum;
if(iInputNum<0||iInputNum>10)
cout<<"输入有误"< }while(iInputNum<0||iInputNum>10);
do
{
//iRandomNum=rand()%101;//产生0~100随机数
//100太慢,改用0~10
iRandomNum=rand()%11;//产生0~10随机数
i=i+1;//计算次数
Sleep(1000); //停顿
cout< if(iInputNum>iRandomNum)
cout<<"小了"< else if(iInputNum cout<<"大了"<
}while(iRandomNum!=iInputNum);
//i=i+1;
cout<<"猜对了,共猜了"< return 0;
}
还有问题联系我

回答3:

#include "stdafx.h"
#include "string.h"

int _tmain(int argc, _TCHAR* argv[])
{
int iInputNumber;
int iOutputChange;
char cInputBuf[10];
int iResultjudge = 0;
iInputNumber=50;
iOutputChange=50;
while (iResultjudge < 1)
{
printf("%d\n",iInputNumber);
scanf("%s",cInputBuf);
if (iOutputChange < 1)
{
iOutputChange=2;
}
if (strlen(cInputBuf) != 1)
{
printf("Input error");
}
if (cInputBuf[0] == '+')
{
iOutputChange = iOutputChange / 2;
iInputNumber = iInputNumber + iOutputChange;
}
else if (cInputBuf[0] == '-')
{
iOutputChange = iOutputChange / 2;
iInputNumber = iInputNumber - iOutputChange;
}
else if (cInputBuf[0] == '=')
{
iResultjudge = 1;
}
if (iOutputChange < 1)
{
iOutputChange=2;
}
}
}

回答4:

#include
#include
#include
using namespace std;
int main(){
srand(time(0));
int a=rand()%100,x=0,n;
while (1){
cin>>n;
x++;
if (n==a){
cout<<"yes,you use "< break;
}
else if (n cout<<"it smaller than answer!";
}
else{
cout<<"it bigger than answer!";
}
cout< }
return 0;
}