怎样用C语言实现一个猜数字游戏
发布日期:2021-05-10 07:44:30 浏览次数:18 分类:精选文章

本文共 3601 字,大约阅读时间需要 12 分钟。

���������������:���������C������������������������

���������������������������������������������������������������������C������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

##������������

������������������������������������������������������������������������������������������������������������������������

###1. ���������������������

������������������������������������������������������������������������������������������������������������"1. ������������" ��� "2. ������������"���

###2. ������������������

������������������������������������������������������������������������������������������������������������������������������������������������

###3. ���������������������

���������������������������������������������������������������������������������������"������������������"���"������������������"���������������"������������"���

###4. ������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������

##������������������

���������������������������������������

###1. include���������

#include 
#include
#include

������������������C���������������������������������������������������������������������������

###2. ������������������

void game()
{
int ret = rand() % 100 + 1; // ������������������������
int num = 0;
while(1)
{
printf("������������������:");
scanf("%d", &num);
if (num > ret)
{
printf("���������������������\n");
}
else if (num < ret)
{
printf("���������������������\n");
}
else
{
printf("������������!\n");
break; // game���������������������������������
}
}
}
  • ������������������������rand()������������1���100������������������������������������������
  • ���������������������������while(1)������������������������������������������������������
  • ������������������������������������������������������������������������������������������

###3. ������������������������

int menu()
{
printf("���������1���������������������2���������������\n");
int choice = 0;
scanf("%d", &choice);
return choice;
}
  • ���������������������������������������������������������������1���2���
  • ������������������������������������������������������������������������������������

###4. ���������������������

int main()
{
srand(time(0)); // Set���������������
while(1)
{
int choice = menu(); // ������������������
if (choice == 1)
{
game(); // ������������
}
else if (choice == 2)
{
printf("���������������\n");
break;
}
}
system("pause");
return 0;
}
  • ���������������������������time(0)���������������������������������������������������������������������������
  • ���������������������������������������������������������
  • ���������������������������������������������2���������������������������������������

������

������������������������������������������������������������������������������������������������������������������������������������������������������������rand()���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������C������������������������������

上一篇:打印n阶的乘法口诀表
下一篇:怎样用C语言实现一个扫雷的游戏?

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月19日 20时09分42秒