猜数(含代码)计算机自动随机生成一个数,用户通过输入数字来猜数,来与随机数进行匹配并显示正确与否

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

以下为程序代码

/*
程序功能:猜数游戏,计算机自动随机生成一个数,‘人’通过输入数字来猜数,来与随机数进行匹配
只有十次输入机会,在十次机会中,猜对即可打印出数字并且显示输入次数。
*/
#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;/*计算机想一个【1-100】之间的数,即为magic */
counter = 0;/*计数器初始化为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;
}

相关文章:

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