猜数(含代码)计算机自动随机生成一个数,用户通过输入数字来猜数,来与随机数进行匹配并显示正确与否
本人实际经验仅供参考
以下是程序的运行结果

以下为程序代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int magic;/计算机想的数/
int guess;/人猜的数/
int counter;/记录人猜数的次数的计数器变量/
srand((unsigned)time(NULL));
magic = rand() % 100 + 1;
counter = 0;
do {
printf(“please guess a magic number:\n”);
scanf_s("%d", &guess);
counter++;/计数器变量的值/
if (guess > magic)
{
printf(“wrong!too high!\n”);
}
else if (guess < magic)
{
printf(“wrong!too low!\n”);
}
else
{
printf(“right!\n”);
printf(“the right number is:%d\n”, magic);
}
}while (guess != magic && counter<10);
printf(“counter= %d\n”, counter);
system(“pause”);
return 0;
}