鱼C论坛

 找回密码
 立即注册
查看: 2221|回复: 7

[已解决]c语言猜数字游戏

[复制链接]
发表于 2022-9-19 21:09:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
因为之前学python小甲鱼教做了猜数字的游戏,在学习C的时候突发奇想用也来做一个,然而因为才开始学,代码运行有问题,我也不知道是哪里出了差错(可能是rand没用对?之前也没用过这个)
代码如下
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int gusse;
    int num = rand();
    int option = 10;//有十次机会
    do
    {
        printf("please input the num you gusse:");
        scanf("%d",&gusse);
   
        if(gusse = num)
        {
            printf("WOW YOU ARE SO SMART!!!");break;
        }
        else if(gusse <= num)
        {
            printf("GUY!You are so SMALL!!!hhh.And you only have %d option!",option);break;
        }
        else if(gusse >= num)
        {
            printf("GUY!You are bigger than that num!And you only have %d option!",option);break;
        }
            option = option - 1;
    }
    while(option > 0);
    printf("GAME OVER!");
    return 0;
}
最佳答案
2022-9-19 22:50:31
看我注释
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main()
  4. {
  5.     int guess;  // 猜测的英文是guess
  6.     int num = rand();
  7.     int option = 10;//有十次机会
  8.     do
  9.     {
  10.         printf("please input the num you guess:");
  11.         scanf("%d",&guess);
  12.    
  13.         if(guess == num)  // 这里要写==,你可能受到python的影响了,认为编译器会不错,但实际上不会报错
  14.         {
  15.             printf("WOW YOU ARE SO SMART!!!\n");break;
  16.         }
  17.         else if(guess < num)  // 这里不要写=的情况
  18.         {
  19.             printf("GUY!You are so SMALL!!!hhh.And you only have %d option!\n",option);  // 既然要让用户继续猜,为什么要退出呢?
  20.         }
  21.         else if(guess > num)  // 这里不要写=的情况
  22.         {
  23.             printf("GUY!You are bigger than that num!And you only have %d option!\n",option);  // 既然要让用户继续猜,为什么要退出呢?
  24.         }
  25.         option--;  //这里用--可提升效率
  26.     }
  27.     while(option > 0);
  28.     printf("GAME OVER!\n");
  29.     return 0;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-19 21:35:26 | 显示全部楼层
    if(gusse = num)  这里是==
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-19 22:50:31 | 显示全部楼层    本楼为最佳答案   
看我注释
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main()
  4. {
  5.     int guess;  // 猜测的英文是guess
  6.     int num = rand();
  7.     int option = 10;//有十次机会
  8.     do
  9.     {
  10.         printf("please input the num you guess:");
  11.         scanf("%d",&guess);
  12.    
  13.         if(guess == num)  // 这里要写==,你可能受到python的影响了,认为编译器会不错,但实际上不会报错
  14.         {
  15.             printf("WOW YOU ARE SO SMART!!!\n");break;
  16.         }
  17.         else if(guess < num)  // 这里不要写=的情况
  18.         {
  19.             printf("GUY!You are so SMALL!!!hhh.And you only have %d option!\n",option);  // 既然要让用户继续猜,为什么要退出呢?
  20.         }
  21.         else if(guess > num)  // 这里不要写=的情况
  22.         {
  23.             printf("GUY!You are bigger than that num!And you only have %d option!\n",option);  // 既然要让用户继续猜,为什么要退出呢?
  24.         }
  25.         option--;  //这里用--可提升效率
  26.     }
  27.     while(option > 0);
  28.     printf("GAME OVER!\n");
  29.     return 0;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-19 23:36:48 | 显示全部楼层
本帖最后由 jackz007 于 2022-9-19 23:44 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. #define count 5

  5. int main(void)
  6. {
  7.         int i , guess , secret                                                                      ;
  8.         time_t t                                                                                    ;
  9.         srand((unsigned) time(& t))                                                                 ;
  10.         secret = rand() % 11                                                                        ; // 产生 0 ~ 10 的随机整数
  11.         for(i = 0 ; i < count ; i ++) {
  12.                 printf("please input the num your gusse (0 ~ 10) : ")                               ;
  13.                 scanf("%d", & guess)                                                                ;
  14.                 if(guess == secret) {
  15.                         printf("WOW YOU ARE SO SMART!!!\n")                                         ;
  16.                         break                                                                       ;
  17.                 } else {
  18.                         if(guess > secret) printf("GUY ! You are bigger than that num !\n")         ;
  19.                         else printf("GUY ! You are so SMALL !!!hhh.\n")                             ;
  20.                         if(i < count - 1)printf("And you only have %d option!\n\n" , count - i - 1) ;
  21.                 }
  22.         }
  23.         printf("GAME OVER !\n")                                                                     ;
  24. }
复制代码

        编译、运行实况
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. please input the num you gusse (0 ~ 10) : 7
  4. GUY ! You are bigger than that num !
  5. And you only have 4 option!

  6. please input the num you gusse (0 ~ 10) : 4
  7. GUY ! You are so SMALL !!!hhh.
  8. And you only have 3 option!

  9. please input the num you gusse (0 ~ 10) : 5
  10. GUY ! You are so SMALL !!!hhh.
  11. And you only have 2 option!

  12. please input the num you gusse (0 ~ 10) : 6
  13. WOW YOU ARE SO SMART!!!
  14. GAME OVER !

  15. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-20 07:13:33 From FishC Mobile | 显示全部楼层
不建议混学,除非你要当程序猿

正是我混学,有时把C的习惯带到Python来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-20 07:14:05 From FishC Mobile | 显示全部楼层
但是你if那点的确要写为==
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-20 17:08:23 From FishC Mobile | 显示全部楼层
zhangjinxuan 发表于 2022-9-20 07:13
不建议混学,除非你要当程序猿

正是我混学,有时把C的习惯带到Python来

本来只想学Python,然而开学后要弄单片机,不得不学c了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-20 18:16:37 | 显示全部楼层
哦~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表