C案例循环问题

时间:2018-02-19 01:44:56

标签: c

这个项目有多个错误。第一个也是最重要的一个必须是我在一个案例中回答一个问题。而不是回到选择案例,而是转向困难。这不会让我退出程序并使平均问题正确。

另一个错误是结束平均问题是正确的。每当我试图跳过问题并结束时,我的程序就会崩溃。除非那是不可能的,否则它只会除以0/0。我应该使用一个开关功能,这是我见过的循环问题。可悲的是,我无法找到一种方法来解决这个问题,或者每一个答案都在我的头上。

#include <stdio.h>

int main()
{
    int option = 0;
    int answer = 0;
    int remainder = 0;
    int ttlProblems = 0;
    int correctAnswers = 0;
    double averageCorrect = 0;
    char difficulty;

    printf("Math Practice Program Menu");

    printf("\n\n1. Addition\n");
    printf("2. Subtraction \n");
    printf("3. Multiplication \n");
    printf("4. Division \n");
    printf("5. Exit\n\n");

    printf("Select an option: ");
    scanf_s("%d", &option);

    while (option != 5)
    {
        switch (option)
        {
        case 1:
            printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
            scanf_s(" %c", &difficulty, 1);
            if (difficulty == 'e')
            {
                printf("3 + 1 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 4)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'm')
            {
                printf("34 + 19 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 53)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'h')
            {
                printf("134 + 119 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 253)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else
            {
                printf("Invalid difficulty set\n");
            }
            break;

        case 2:
            printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
            scanf_s(" %c", &difficulty, 1);


            if (difficulty == 'e')
            {
                printf("3 - 1 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 2)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'm')
            {
                printf("34 - 19 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 15)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'h')
            {
                printf("134 - 119 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 15)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else
            {
                printf("Invalid difficulty s\n");
            }
            break;

        case 3:
            printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
            scanf_s(" %c", &difficulty, 1);


            if (difficulty == 'e')
            {
                printf("3 * 1 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 3)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'm')
            {
                printf("34 * 19 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 646)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'h')
            {
                printf("134 * 119 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);

                if (answer == 15946)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else
            {
                printf("Invalid difficulty set\n");
            }
            break;

        case 4:
            printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
            scanf_s(" %c", &difficulty, 1);

            if (difficulty == 'e')
            {
                printf("9 / 2 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);
                printf("Enter any remainder: ");
                scanf_s("%d", &remainder, 1);

                if (answer == 4 && remainder == 1)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'm')
            {
                printf("34 / 19 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);
                printf("Enter any remainder: ");
                scanf_s("%d", &remainder, 1);

                if (answer == 1 && remainder == 15)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else if (difficulty == 'h')
            {
                printf("134 / 19 = ? Enter answer: ");
                scanf_s("%d", &answer, 1);
                printf("Enter any remainder: ");
                scanf_s("%d", &remainder, 1);

                if (answer == 7 && remainder == 1)
                {
                    ++ttlProblems;
                    ++correctAnswers;
                    printf("Correct!\n");
                }
                else
                {
                    ++ttlProblems;
                    printf("Sorry, Incorrect\n");
                }
            }

            else
            {
                printf("Invalid difficulty set\n");
                break;
            }
            break;

        case 5:
            break;

        default:
            printf("Not a valid option.\n");
        }
    }
    averageCorrect = ttlProblems / correctAnswers;
    printf("You attempted %d problems and got %d correct\nFor a percentage of %.2f correct.", ttlProblems, correctAnswers, averageCorrect);
}

1 个答案:

答案 0 :(得分:0)

目前,您的代码要求用户在while循环之前输入选项类型,因此它只会询问一次并在每次循环迭代时将该值用于该选项。将选项输入scanf_s("%d", &option);移动到while循环的开头可以解决您的第一个问题。

关于除以零,这将导致程序崩溃,因为它不可能。在执行除法之前,您可能想要检查correctAnswers是否为零。