【问题标题】:Is there a more efficient way to create a counter for a quiz game using C? [closed]有没有更有效的方法来使用 C 为问答游戏创建计数器? [关闭]
【发布时间】:2017-10-30 13:57:34
【问题描述】:

我正在尝试开发一个测验类型的数学游戏,用户必须在其中解决 1-5 个问题。我想添加一个计数器,在测验结束时显示所有正确和错误的答案,到目前为止,我一直在使用 if else 语句来做这件事,但我觉得应该有一种更有效的方法来做这件事。我的代码:

    if ( questions == 2 )
    { 
        printf (" What is 2 + 2\n");
        scanf("%d",&A1);
            if( A1 == 4 )       
            {
                    correct ++;
                }
                else
                {
                    incorrect ++;
            }

        printf (" What is 5 + 2\n");
        scanf("%d",&A2);
            if( A2 == 7 )       
            {
                correct ++;
            }
            else
            {
                incorrect ++;
            }

    }

这是我为用户可以选择的每个选项编写了 5 次相同内容的代码。非常感谢所有帮助,在此先感谢!

【问题讨论】:

  • 只使用一个计数器?如果任何不“正确”的东西是“不正确的”,我认为不需要两个计数器。
  • switch(问题){ case 1: ... break;案例 2:……中断;案例 3:……中断;案例 4:……中断;案例 5:……中断;默认值:/* 输入应该是 1...5*/ exit(1); }
  • 我使用 2 个计数器,因为我想显示用户玩的所有回合中正确和错误答案的数量
  • 那么你认识运营商-
  • 你的问题不是写一个函数,你可以用不同的问题字符串和正确的答案值调用5次。

标签: c counter


【解决方案1】:

您可以使用问题的总数减去正确答案来获得错误答案的数量。这是一个示例:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   char arQuestions[5][20] = { "2 + 2 =",
                               "2 * 2 =",
                               "2 - 2 =",
                               "2 / 2 =",
                               "2 ^ 2 ="};
   int answers[5] = {4,4,0,1,4};
   int i = 0;
   int answer  = 0;
   int correct = 0;

   for(;i<5;++i)
   {
       printf("%s ", arQuestions[i]);
       if( 1 == scanf("%d", &answer))
         if(answer == answers[i])
            correct++;
       printf("correct<%d> incorrect<%d>\n", correct, (i+1)-correct);
   }
   return(0);
}

【讨论】:

    【解决方案2】:

    当您需要针对多个值检查一个变量时,可以使用 Switch 语句(就像您对 if ( questions == 0...5 ) 所做的那样。为了不一遍又一遍地重写相同的问题,我们可以使用 switch 语句自然溢出的方式根据我们想要的问题数量,进入下一个“级联”案例。

    最后,正如其他人指出的那样,我们不需要分隔变量来跟踪正确或错误的答案;只需跟踪正确答案,最后错误答案将等于questions - correct。 综上所述,我们得到了如下所示的内容:

    int questions;
    printf("Number of questions?\n");
    scanf("%d",&questions);
    
    int correct = 0;
    int answer = 0;
    
    switch(questions) {
        case 5:
            printf("What is 2 + 2?\n");
            scanf("%d",&answer);
            if(answer == 4)       
            {
                correct++;
            }
            //note - no break at end of each case
        case 4:
            //ask another question
        case 3:
            //ask another question
        case 2:
            //ask another question
        case 1:
            //ask another question
        default:
            break;
    }
    printf("Results:\nCorrect = %d\nIncorrect = %d", correct, questions - correct);
    

    【讨论】:

    • 感谢这真的有帮助!这是我正在做的大学评估,当我们确实切换语句时我错过了,我一直在尝试学习它,但无法真正了解如何使用它们。但是,关于它的唯一一个问题是用户为变量“问题”设置的值?
    • 将 switch 想象成多个 if() else if() 语句。基本上它说,“如果问题是 5,则在第一个案例之后做事情;否则如果是 4,那么在第二个案例之后做事情,等等。”。通常,您会在每个案例结束时中断,以便交换机不会运行任何其他案例。但是因为我们不会在我们的案例之后中断,所以它会在案例 5 之后执行操作,案例 4 中的内容,案例 3 中的内容,等等。
    • 在实践中,建议在每个case 末尾添加break;Duff's device 是一个特殊的技巧。
    • 好吧,这对我来说更有意义了,非常感谢!
    • 我不认为将 if-elseif 梯形图更改为 switch-case 是有用的更改。这只是化妆品。问题是 5x 复制粘贴一段代码。
    【解决方案3】:

    还有另一种可能更有效的选择

    您可以创建correctincorrect 全局变量或创建指向它们的指针,并创建另一个函数来检查输入的答案是否正确并相应地更新correctincorrect

    // initiate global variable's
    int correct = 0, incorrect = 0;
    
    void checkanswer(int var1, int var2, int useranswer)
    {
        if(useranswer == var1 + var2)
            correct++;
        else
            incorrect++;
    }
    
    int main(void)
    {
    ...
        printf (" What is 2 + 2\n");
        scanf("%d", &A1);
        checkanswer(2, 2, A1);
    //next question
    }
    

    这样,您无需重复自己,而是使用您编写的函数。

    其他一些事情:

    • 尝试找到scanf 的替代方法,它是一个危险的函数,会使您的代码变得易变。请参阅this 和/或搜索更多答案,因为该主题在网上有很多已回答的问题。
    • 我也写了一个数学游戏,如果你想要一些与你正在写的东西类似的例子。在我的游戏中,您需要获得 10 分,并且问题在任何游戏中都是随机的。如果您有兴趣,请参阅here

    希望我能帮上忙! 如果您有任何问题或其他任何问题,请随时向我询问我的游戏:)

    【讨论】:

    • 您也可以将 printf 和 scanf 调用合并到函数中,假设格式字符串始终为“%d”。此外,正确使用 scanf 是非常安全的。它返回参数列表中已成功为其赋值的变量数量,因此您可以轻松检测无效输入并做出适当响应。 fgets/sscanf 或 fgets/strtoul 也有自己的问题。编写安全代码不仅仅是避免一系列邪恶函数的问题。所有这三种方法在某些时候都是最佳选择,而在其他时候则是坏主意。
    • 知道了,谢谢你的意见,我还在学习:)
    【解决方案4】:

    这是一个更通用的版本,它实际计算方程式并根据用户的答案进行检查。

    #include <stdio.h>
    
    typedef enum
    {
      ADD,
      SUB,
      MUL,
      DIV,
    } operation_t;
    
    typedef struct
    {
      int op1;
      int op2;
      operation_t op;
    } equation_t;
    
    char operation_to_char (operation_t op)
    {
      const char CH_OP[] = {'+', '-', '*', '/'};
      return CH_OP[op];
    }
    
    int solve (const equation_t* eq)
    {
      switch(eq->op)
      {
        case ADD: return eq->op1 + eq->op2;
        case SUB: return eq->op1 - eq->op2;
        case MUL: return eq->op1 * eq->op2;
        case DIV: return eq->op1 / eq->op2;
      }
      return 0; // to silence compiler warning, should never happen
    }
    
    int main (void)
    {
      const equation_t EQUATIONS[] = 
      {
        {1, 1, ADD},
        {2, 2, ADD},
        {1, 1, SUB},
        {2, 2, MUL},
        {9, 3, DIV},
      };
      const size_t EQUATIONS_N = sizeof(EQUATIONS)/sizeof(*EQUATIONS);
    
      for(size_t i=0; i<EQUATIONS_N; i++)
      {
        printf("What is %d %c %d? ", 
                 EQUATIONS[i].op1,  
                 operation_to_char(EQUATIONS[i].op),
                 EQUATIONS[i].op2);
    
        int answer;
        scanf("%d", &answer);
        getchar(); // discard line feed
    
        int solution = solve(&EQUATIONS[i]);
        if(answer == solution)
        {
          puts("Correct");
        }
        else
        {
          printf("Incorrect, the solution is %d\n", solution);
        }
      }
    }
    

    请注意,此代码对用户输入没有错误处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 2017-11-25
      • 1970-01-01
      相关资源
      最近更新 更多