下面是我编写的do-while循环。当我运行它时,前两个案例完成了他们的工作,运行得很好。然而。第三种情况应该退出程序,但它什么也不做,只是返回do while循环开始时的printf语句序列。关于我做错了什么有什么建议吗?

do
{
    printf("Choose one of the following (1, 2, or 3) \n");
    printf("1. Find GCD of two positive integers\n");
    printf("2. Sort 3 integers in the ascending order\n");
    printf("3. Quit the program\n");
    printf("Please enter your choice: ");
    scanf("%d", &option);
    switch (option)
    {
        case 1:
            gcd(p, q);
            printf("\nDo you want to try again? Say Y(es) or N(o): ");
            getchar();
            response = getchar();
            break;

        case 2:
            sort(p, q, r);
            printf("\nDo you want to try again? Say Y(es) or N(o): ");
            getchar();
            response = getchar();
            break;

        case 3:
            break;
    }
}
while (response == 'Y' || response == 'y'); //Condition that will determine whether or not the loop continues to run.
printf("\nThank you for using my progam. Goodbye!\n\n");
return 0;
}

最佳答案

响应变量保持Y或Y,而环路从不退出。
添加

response = 'x'; //or something that isn't Y or y

休息前;案例3:选项。

关于c - C中有三种情况的Switch语句。第三种情况无法正常运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14897102/

10-10 19:35