编程实现:由系统生成一个10以内的随机整数,用户从键盘上输入一个10以内的整数,如果两者相等输出“OK”

否则输出“ERROR”。
2024-11-22 03:27:31
推荐回答(2个)
回答1:

#include
#include
#include
int main(void)
{
int a,b,i=0;
int t=0,d=10;
srand(time(NULL)); /*初始化随机数种子*/
a=rand()%(d-t+1)+t; /*生成一个[0 10]区间内的整数*/
printf("you have three times to answer:\n");
do{scanf("%d\n",&b);
i++;
if(a==b) printf("you are right\n");
else printf("error\n");
}while((a!=b)&&(i<3));
if(i=3) printf("the right answer is:%d\n",a);
}

回答2:

#include
#include
using namespace std;

int randNum();

void main()
{
int num;
int randnum = randNum();
cout << randnum;
cin >> num;
if (randnum != num )
{
cout << "ERROR";
}
else
cout << "OK";

}
int randNum()
{
srand(time(NULL));
int num = rand();
num = num % 10;
return num;
}