有没有更有效的方法来使用 C 为问答游戏创建计数器?

Is there a more efficient way to create a counter for a quiz game using C?

我正在尝试开发一种测验类型的数学游戏,用户必须在其中解决 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 次相同内容的代码。非常感谢所有帮助,在此先感谢!

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

#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);
}

当您需要根据多个值检查一个变量时,可以使用 Switch 语句(就像您对 if ( questions == 0...5 ) 所做的那样)。为了不一遍又一遍地重写相同的问题,我们可以使用switch 语句自然会溢出到下一个 case 到 "cascade" 取决于我们想要的问题数量。

最后,正如其他一些人所指出的,我们不需要分离变量来跟踪正确或错误的答案;只跟踪正确答案,最后错误答案等于 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);

还有一个可能更有效的选择

您可以创建 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

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

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

#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);
    }
  }
}

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