欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用C语言编写一个简单的猜数字游戏

最编程 2024-08-14 18:15:07
...

直接上源码

#include<stdio.h>
#include<time.h>
#include<Windows.h>
void game()//猜数字游戏功能函数
{
  int num = 0, input = 0;
  num = rand() % 100 + 1;//获取1-100的一个随机数
  while (1)//利用if循环判断
  {
    printf("请输入数字\n");
    scanf_s("%d", &input);
    if (input == num)
    {
      printf("恭喜,你猜的数字是对的\n");
      break;
    }
    else if (input < num)
    {
      printf("猜小了\n");
    }
    else
    {
      printf("猜大了\n");
    }
  }
}
void menu()
{
  printf("**************************************\n");
  printf("**********1.game        0.exit**********\n");
  printf("**************************************\n");
}
int main()
{
  int choice=0;
  srand((unsigned int)time(NULL));//防止出现的随机数一样
  do//利用循环实现可以多次玩
  {
    menu();
    printf("请输入选项\n");
    scanf_s("%d", &choice);
    switch (choice)
    {
    case 1:game();
      break;
    case 0:
      break;
    default:
      printf("选择错误\n");
      break;
    }
 
  } while (choice);
  return 0;
}

原文链接:https://blog.****.net/ningningmingming/article/details/78094473

END