试图让变量等于C

时间:2017-10-17 20:30:01

标签: c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
  srand(time(NULL));
  int score=0;
  int loop=0;
  int randNUM1=0;
  char randSYMB1=0;
  int randNUM2=0;
  int correct_answer=0;
  int answer=0;
  for(loop=1;loop<=10;loop++)
  {
     randNUM1 = rand()%10; printf("%c", "0123456789"[randNUM1]);
     randSYMB1 = rand()%4; printf("%c", "/*+-"[randSYMB1]);
     randNUM2 = rand()%10; printf("%c\n", "0123456789"[randNUM2]);
    printf("What is your answer?");
    scanf("%d", &answer);
    correct_answer=randNUM1/randNUM2 || randNUM1*randNUM2 || 
randNUM1+randNUM2 || randNUM1-randNUM2;

        if (randSYMB1=="/")
        {
           printf("The correct answer is %d\n", randNUM1/randNUM2);
           scanf("%d", &correct_answer);
           score++;
        }
        else if (randSYMB1=="*")
        {
           printf("The correct answer is %d\n", randNUM1*randNUM2);
           scanf("%d", &correct_answer);
           score++;
        }
        else if (randSYMB1=="+")
        {
           printf("The correct answer is %d\n", randNUM1+randNUM2);
           scanf("%d", &correct_answer);
           score++;
        }
        else if (randSYMB1=="-")
        {
           printf("The correct answer is %d\n", randNUM1-randNUM2);
           scanf("%d", &correct_answer);
           score++;
        }
 printf("Your score is %d\n", score);

  }

}

该代码用于简单计算游戏,其中向用户询问10个问题,其中包括随机生成数字,然后是符号然后另一个数字,然后存储答案,然后告知用户他们是否答案是正确的,在10个问题的最后,用户得到10分。我的代码似乎随机生成数字和符号,循环10个问题,但是,代码似乎没有将IF语句考虑在内,这意味着没有显示正确的答案,并且在每个问题为0之后输出给用户的分数。因此,从我的teel中可以看出IF语句存在问题,但我无法理解,那是什么问题。我也尝试过使用CASE语句,但问题仍然是一样的;该计划不考虑这些陈述。我无法理解为什么程序会这样做。这是FOR循环中的问题,还是&#34; correct_answer&#34;的问题。变量?请帮忙!

3 个答案:

答案 0 :(得分:2)

Benny,你所遇到的大部分困难都源于显然只是从编码开始。这没有什么不对,如果你已经知道了,那么你就不需要学习......(而且你从来没有真正学过如何编程,硬件改变,标准改变等等。它&# 39;旅程不是种族)

首先,如果您有选项,请在开始执行语句之前声明所有变量。这将使您的代码可以移植到仍然使用C89标准的编译器(所有windoze版本通过Win7),例如。

    int score=0,
        loop=0,
        randNUM1=0,
        randNUM2=0,
        correct_answer=0,
        answer=0;
    char *symbol = "/*+-",  /* use a string literal */
        randSYMB1=0;

    srand (time(NULL));     /* now begin executing statements */

这是一种风格问题,但如果你稍微打开间距,它会使你的代码更具可读性(特别是对于年长的眼睛......)。虽然您可以通过在每行上填写两个表达式或使用逗号运算符来保存行 - 不要。它只会让你的代码更难阅读(对你和其他任何人)。当然,如果它是愚蠢的,那么你就可以逃脱它,例如。

if (!a) b = 1;

否则,将每个陈述放在它自己的行上:

    for (loop = 1; loop <= 10; loop++)
    {
        /* separate all statements on it own line for readability. */
        randNUM1 = rand() % 10;         /* you have an int, use it */
        printf ("%d", randNUM1);

        randSYMB1 = symbol[rand() % 4]; /* get char from symbol string */
        printf (" %c ", randSYMB1);

        randNUM2 = rand()%10;
        printf ("%d\n", randNUM2);

        printf("What is your answer?: ");
        if (scanf("%d", &answer) != 1) {    /* validate ALL user input */
            fscanf (stderr, "error: invalid input - answer.\n");
            return 1;
        }

接下来,问题的关键在于如何处理检查数学运算和用户答案的​​逻辑。所有逻辑都基于randSYMB1 = symbol[rand() % 4];(我的更改)生成的随机字符。

你如何根据一组角色中的哪一个采取不同的行动?好吧,您可以将整个ifelse if语句串联在一起,或者只使用switch语句:

        switch (randSYMB1) {
            case '/':   correct_answer = randNUM1 / randNUM2;
                        break;
            case '*':   correct_answer = randNUM1 * randNUM2;
                        break;
            case '+':   correct_answer = randNUM1 + randNUM2;
                        break;
            case '-':   correct_answer = randNUM1 - randNUM2;
                        break;
        }

这会留下一个简单的比较来确定是否输入了正确的答案:

        if (answer == correct_answer) {
            printf ("Correct!\n");
            score++;
        }
        else
            printf ("Incorrect. Correct answer: %d\n", correct_answer);

        printf ("Your current score is:  %d\n\n", score);

注意:用于除法,用户必须输入整数除法将产生的结果)

完全放弃,您可以将代码简化为:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void) {

    int score=0,
        loop=0,
        randNUM1=0,
        randNUM2=0,
        correct_answer=0,
        answer=0;
    char *symbol = "/*+-",  /* use a string literal */
        randSYMB1=0;

    srand (time(NULL));

    for (loop = 1; loop <= 10; loop++)
    {
        /* separate all statements on it own line for readability. */
        randNUM1 = rand() % 10;         /* you have an int, use it */
        printf ("%d", randNUM1);

        randSYMB1 = symbol[rand() % 4]; /* get char from symbol string */
        printf (" %c ", randSYMB1);

        randNUM2 = rand()%10;
        printf ("%d\n", randNUM2);

        printf("What is your answer?: ");
        if (scanf("%d", &answer) != 1) {    /* validate ALL user input */
            fscanf (stderr, "error: invalid input - answer.\n");
            return 1;
        }

        switch (randSYMB1) {
            case '/':   correct_answer = randNUM1 / randNUM2;
                        break;
            case '*':   correct_answer = randNUM1 * randNUM2;
                        break;
            case '+':   correct_answer = randNUM1 + randNUM2;
                        break;
            case '-':   correct_answer = randNUM1 - randNUM2;
                        break;
        }

        if (answer == correct_answer) {
            printf ("Correct!\n");
            score++;
        }
        else
            printf ("Incorrect. Correct answer: %d\n", correct_answer);

        printf ("Your current score is:  %d\n\n", score);
    }

    return 0;
}

示例使用/输出

C:\Users\david\Documents\dev\src-c\tmp>bin\mathtest.exe
9 + 3
What is your answer?: 12
Correct!
Your current score is:  1

8 * 8
What is your answer?: 64
Correct!
Your current score is:  2

5 + 7
What is your answer?: 12
Correct!
Your current score is:  3

6 - 4
What is your answer?: 2
Correct!
Your current score is:  4

6 / 4
What is your answer?: 1
Correct!
Your current score is:  5

7 * 9
What is your answer?: 63
Correct!
Your current score is:  6

5 / 6
What is your answer?: 0
Correct!
Your current score is:  7

8 * 2
What is your answer?: 16
Correct!
Your current score is:  8

0 / 1
What is your answer?: 0
Correct!
Your current score is:  9

9 + 7
What is your answer?: 16
Correct!
Your current score is:  10

仔细看看,如果您有其他问题,请告诉我。

答案 1 :(得分:0)

如果从不执行,因为randSYMB1是0-3之间的数字,而不是&#34; / * - +&#34;。我建议先播种随机播种,但在这种情况下它并不重要。

答案 2 :(得分:0)

您的代码存在一些问题。 randSYMB1的值为0到3,其中没有一个对应于/*+-的ascii值。

将if语句更改为:

if (randSYMB1==0)

相应的其余部分。此外,你永远不应该写像:

char c;
...
if(c == "x")

你应该使用单引号:

if(c == 'x')

这一行:

correct_answer=randNUM1/randNUM2 || randNUM1*randNUM2 ||
        randNUM1+randNUM2 || randNUM1-randNUM2;

是完全的乱码。在if语句中移动正确值的计算。

此:

      printf("%c", "0123456789"[randNUM1]);

有效,但它非常疯狂。这样更好:

      printf("%d", randNUM1);

我认为它可以作为randSYMB1的快速解决方法,但对操作数没有任何理由。 这是for循环的工作版本:

  for(loop=1;loop<=10;loop++)
    {
      randNUM1 = rand()%10; 
      printf("%d", randNUM1);
      randSYMB1 = rand()%4; 
      printf("%c", "/*+-"[randSYMB1]);
      randNUM2 = rand()%10; 
      printf("%d\n", randNUM2);
      printf("What is your answer?");
      scanf("%d", &answer);

          if (randSYMB1==0)
            correct_answer=randNUM1/randNUM2;
          else if (randSYMB1==1)
            correct_answer=randNUM1*randNUM2;
          else if (randSYMB1==2)
            correct_answer=randNUM1+randNUM2;
          else if (randSYMB1==3)
            correct_answer=randNUM1-randNUM2;
          printf("The correct answer is %d\n", correct_answer);
          if(answer == correct_answer)
            score++;
          printf("Your score is %d\n", score);
    }