0

“我遇到的实际问题是它在给出问题数量后停止运行。我已将“%d%”更正为“%d”,但仍然存在该问题”

我目前正在做一个小型数学游戏的项目。我需要提示一个难度(简单、困难或退出)级别,然后是一个类别(加法、减法、乘法和除法。我的菜单正常工作,但我似乎无法得到以下任何内容

printf (" How many questions would you like to attempt?\n");
scanf ("%d%", &numofquestions);

去工作。谁能告诉我我做错了什么?以下是我到目前为止的完整代码,那里有一些不必要的项目,但据我所知,它们不是问题。

/* Program Description Goes Here */
#include <stdio.h>
#include <stdlib.h>

 /* function main begins program execution */
int main( void )
{
    /* Main Code Area For Program */
int difficulty = -1;
int category = 0;
int numofquestions;
int ca = 0; /* Correct Answer */
int cc; /* Correcnt Counter */
int ran1;
int ran2;
int random1 = 0;
int random2 = 0;
int usera; 
int i;
/* Welcome mesage */
srand ( time (NULL));

printf ( "Welcome to the math challenge.\n"); 


while ( difficulty != 1 && difficulty != 2 && difficulty !=0){ 

    printf ("Select 1 for Easy\n");
    printf ("Select 2 for Hard\n");
    printf ("Select 0 to Quit\n");
    scanf ( "%d", &difficulty);
}

        while ( category != 1 && category != 2 && category != 3 && category != 4){

    category = getchar();
    switch (category){
        case 'A':
        case 'a':
            category = 1;
            break;
        case 'S':
        case 's':
            category = 2;
            break;
        case 'M':
        case 'm':
            category = 3;
            break;
        case 'D':
        case 'd':
            category = 4;
            break;
        case '/n':
        case '/t':
        case ' ':
            break;
        default:
            printf ("Enter A for Addition\n");
            printf ("Enter S for Subtraction\n");
            printf ("Enter M for Multiplication\n");
            printf ("Enter D for Division\n");
    }
}

        printf (" How many questions would you like to attempt?\n");
        scanf ("%d%", &numofquestions);


        for (i = numofquestions; i<=0; --i){/* start for */

            if ( difficulty == 1){ /* start if */
                random1 = (rand () % 10);
                random2 = (rand () % 10);

                }/* end if */ 

            if ( category == 1){/* start if */
                printf ( "%d + %d = ?" , random1 , random2 );
                ca = random1 + random2;
                printf ( "Your response\n");
                scanf ( "%d", &usera);
                                }/* end if */

            if ( usera == ca ){ /* start if */
                printf (" %d\n ", ca);
            }/* end if */


        /* end for */}


return 0; /* indicate that program ended successfully */
/* end function main */
}
4

3 回答 3

1

那额外的 %d* % * 是故意的吗?

scanf ("%d%", &numofquestions);

for (i = numofquestions; i<=0; --i){/* start for */ 这应该是 for (i = numofquestions; i>=0; --i) 我想!

于 2013-03-21T17:40:54.733 回答
1

删除重复项%

scanf("%d%", &numofquestions);

应该:

scanf("%d", &numofquestions);
于 2013-03-21T17:38:49.977 回答
0

getchar()返回一个 int 和你的 switch 语句你的检查字符。http://www.cplusplus.com/reference/cstdio/getchar/

%d%应该是%dfor (i = numofquestions; i<=0; --i){你得到一个int。这只会在数字为 0 或更少时运行,然后将永远运行。

于 2014-10-10T20:23:28.090 回答