简单计算器无法按预期工作 - 初学者帮助(C)

时间:2016-10-30 15:26:29

标签: c

关于这一点,我是新手(这是我写过的第一个程序),我不确定这些论坛是否是正确的问题,但我想知道是否有人可以帮忙解决我的问题。

当我从计算器中选择问卷选项时,第一个问题正常工作,然后程序就结束了。嵌套的switch语句不会运行。

int main () {

    /** Declaring variables (multiple variables can be declared together providing they are the same data type). **/
    int selectOperation,firstValue,secondValue,questInput,userScore,nextQuestion;

    printf("\n***Welcome back, sir!***\n");

    do {
            printf("\n***Please select your desired operation by inputting the associated value***\n\n");
            printf(" [1]: Addition \n [2]: Subtraction \n [3]: Division \n [4]: Multiplication \n [5]: Questionnaire \n");

            /** Asks the user to input an integer and stores the value in the selectOperation variable. **/
            printf("\nEnter value: ");
            scanf("%d", &selectOperation);

            /** While the selectOperation variable is not equal to 1, 2, 3, 4 or 5 continue to run this loop. **/
            /** The loop will continue to run until the user selects an operation. **/

    } while((selectOperation != 1) && (selectOperation != 2) && (selectOperation != 3) && (selectOperation != 4) && (selectOperation != 5));

    if ((selectOperation == 1) || (selectOperation == 2) || (selectOperation == 3) || (selectOperation == 4)) {

        /** Input first value **/
        printf("\nPlease enter the first value you would like to process:\n");
        printf("\nEnter value: ");
        scanf("%d", &firstValue);

        /** Input second value **/
        printf("\nPlease enter the second value you would like to process:\n");
        printf("\nEnter value: ");
        scanf("%d", &secondValue);
}

    /** If the selectOperation variable is equal to the case value the code for that case will be ran, for 
        example if selectOperation's current value is 1 the text "You have selected addition" will print." **/

    switch(selectOperation) {

            case 1: /** Operation: Addition **/
                    printf("\nYou have selected addition! Your answer can be found below:\n\n");
                    /** Adds the stored values of the variables firstValue and secondValue and outputs the workings/result. **/
                    printf("%d + %d = %d\n",firstValue,secondValue,firstValue+secondValue);
                    break;

            case 2: /** Operation: Subtraction **/
                    printf("\nYou have selected subtraction! Your answer can be found below:\n\n");
                    /** Subtracts the value of secondValue from firstValue and outputs the workings/result. **/
                    printf("%d - %d = %d\n",firstValue,secondValue,firstValue-secondValue);
                    break;

            case 3: /** Operation: Division **/
                    printf("\nYou have selected division! Your answer can be found below:\n\n");
                    /** Divides firstValue by secondValue and outputs the workings/result. **/
                    printf("%d / %d = %d\n",firstValue,secondValue,firstValue/secondValue);
                    break;


            case 4: /** Operation: Multiplication **/
                    printf("\nYou have selected multiplication! Your answer can be found below:\n\n");
                    /** Multiplies firstValue by secondValue and outputs the workings/result. **/
                    printf("%d * %d = %d\n",firstValue,secondValue,firstValue*secondValue);
                    break;



            case 5: /** Questionnaire **/
                    printf("\nQuestion 1: What is 48 * 3?\n");
                    printf("\nEnter value: ");
                    scanf("%d", &questInput);
                    if (questInput == 144) {
                        printf("That's correct!\n");
                        userScore = userScore  + 1;
                        nextQuestion = nextQuestion + 1;
                }
                    else {
                        printf("That's incorrect!\n");
                        nextQuestion = nextQuestion + 1;
                }

                    while(nextQuestion > 0 && nextQuestion < 5) {

                        switch(nextQuestion) {

                                case 1:
                                        printf("\nQuestion 2: What is 36 / 6?\n");
                                        printf("\nEnter value: ");
                                        scanf("%d", &questInput);
                                            if (questInput == 6) {
                                                printf("That's correct!\n");
                                                userScore = userScore  + 1;
                                                nextQuestion = nextQuestion + 1;
                                            }
                                            else {
                                                printf("That's incorrect!\n");
                                                nextQuestion = nextQuestion + 1;
                                            }

                                        break;

                                case 2:
                                        printf("\nQuestion 3: What is 1526 + 49?\n");
                                        printf("\nEnter value: ");
                                        scanf("%d", &questInput);
                                            if (questInput == 1575) {
                                                printf("That's correct!\n");
                                                userScore = userScore  + 1;
                                                nextQuestion = nextQuestion + 1;
                                            }
                                            else {
                                                printf("That's incorrect!\n");
                                                nextQuestion = nextQuestion + 1;
                                            }

                                        break;

                                case 3:
                                        printf("\nQuestion 4: What is 5 * (5 + 15)?\n");
                                        printf("\nEnter value: ");
                                        scanf("%d", &questInput);
                                            if (questInput == 100) {
                                                printf("That's correct!\n");
                                                userScore = userScore  + 1;
                                                nextQuestion = nextQuestion + 1;
                                            }
                                            else {
                                                printf("That's incorrect!\n");
                                                nextQuestion = nextQuestion + 1;
                                            }

                                            break;

                                case 4:
                                        printf("\nQuestion 5: What is 30 + 21?\n");
                                        printf("\nEnter value: ");
                                        scanf("%d", &questInput);
                                            if (questInput == 51) {
                                                printf("That's correct!\n");
                                                userScore = userScore  + 1;
                                                nextQuestion = nextQuestion + 1;
                                            }
                                            else {
                                                printf("That's incorrect!\n");
                                                nextQuestion = nextQuestion + 1;
                                            }

                                            break;
        }

        }

    }
}

1 个答案:

答案 0 :(得分:0)

这就是我的意思,改变

int selectOperation,firstValue,secondValue,questInput,userScore,nextQuestion;

int selectOperation,firstValue,secondValue,questInput,userScore = 0,nextQuestion = 0;

选项1到4执行一次,然后程序退出。选项5询问一系列问题,然后程序退出。