|
发表于 2020-10-10 19:15:06
|
显示全部楼层
本帖最后由 baige 于 2020-10-10 19:22 编辑
- #include<stdio.h> // stdio.h 头文件是系统自带,用尖括号,不用引号
- #include<conio.h> // getch() 需要这个头文件
- int main(void)
- {
- int total = 21;
- int shengyu, yours, computer;
- char another;
- printf("****************************************\n");
- printf("***** 起始牙签个数为%d *****\n", total);
- printf("***** 每次只能拿走1-3个 *****\n");
- printf("***** 谁拿最后一个谁就输了 *****\n");
- printf("\n");
- printf("\n明白了?按任意键开始吧......");
- getch(); // 没有添加头函数 conio.h
- shengyu = total;
- while (shengyu != 0)
- {
- do //只能拿1-3个
- {
- printf("\n当前牙签数为%d\n", shengyu);
- printf("请输入你拿走的牙签数(1-3): ");
- scanf("%d", &yours);
- if (yours >= 1 && yours <= 3)
- break;
- else
- printf("你每次只能拿1到3个。\n");
- } while (1);
- shengyu = shengyu - yours;
- if (shengyu < 0)
- {
- printf("\n\n你不按规则来呀! \n");
- shengyu = shengyu + yours;
- continue;
- }
- if (shengyu == 0)
- {
- printf("你输了,再来不? (Y/N)");
- fflush(stdin);
- another = getchar();
- if (another == 'Y' || another == 'y')
- {
- shengyu = total;
- }
- else
- {
- printf("\n\n再见,以后再来玩呀!\n");
- break;
- }
- }
- else
- {
- computer = 4 - yours;
- printf("\n电脑拿走了%d个牙签\n",computer);
- shengyu = shengyu - computer;
- }
- }
- return 0;
- }
复制代码 |
评分
-
查看全部评分
|