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

时间:2017-10-30 13:57:35

标签: c counter

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

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

当您需要针对多个值检查一个变量时,可以使用切换语句(就像使用if ( questions == 0...5 )一样。为了不反复重写相同的问题,我们可以使用switch语句的方式根据我们想要的问题数量,自然会超出下一个案例到&#34;级联&#34;

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

答案 2 :(得分:1)

还有另一种选择可能会更有效率

您可以制作"apple iPhone 7"correct全局变量或创建指向它们的指针,并创建另一个函数来检查输入的答案是否正确并更新incorrect或{{1}相应地:

correct

这样,您可以使用您编写的功能,而不是重复自己。

其他一些事情:

  • 尝试找到incorrect的替代方案,这是一个危险的功能,使您的代码可以转换。请参阅this和/或搜索更多答案,因为此主题在线有很多已回答的问题。
  • 我也写了一个数学游戏,如果你想要一些类似于你写作的东西的另一个例子。在我的游戏中,你需要得到10分,任何游戏中的问题都是随机的。如果您有兴趣,请参阅here

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

答案 3 :(得分:1)

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

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

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