Yes/No 在 C 中使用 while 循环和数学运算程序进行循环

Yes/No loop using while loop in C with a math operation program

首先,我是 C(和任何编程)和 Stack Overflow 的初学者,如果之前有人问过这样的问题,我很抱歉。

所以我在循环部分的数学运算代码上遇到了问题。当我输入 N 或 Y 时,程序就像我输入了不同的选项一样,并提示我必须输入 Y 或 N。

这是代码。对不起,如果它是一团糟,这就是我目前所知道的。

#include <stdio.h>
int main() {
    while (1) {
        int choice1, choice2, num1, num2;
        printf("\n  [1] Addition\n  [2] Subtraction\n  [3] Multiplication\n  [4] Division\n");
        printf("\n Pick a choice: ");
        scanf("%d", &choice1);
        printf("\n Give a number: ");
        scanf("%d", &num1);
        printf("\n Give another number: ");
        scanf("%d", &num2);
        switch (choice1) {
            case 1:
            printf("Sum is %d", num1+num2);
            break;
            case 2:
            printf("Difference is %d", num1-num2);
            break;
            case 3:
            printf("Product is %d", num1*num2);
            break;
            case 4:
            printf("Quotient is %d", num1/num2);
            break;
            default:
            printf("Please select a valid operation");
        }
            printf("\n Try another operation [y/n]: ");
            scanf("%d", &choice2);
            if (choice2 == 'y' || choice2 == 'Y') {
                printf("Retrying...");
                break; }
            else if (choice2 == 'n' || choice2 == 'N') {
                printf("Exiting...");
                break; }
            else {
                printf("Pick y or n only");
                break;
            }
    }
    return 0;
}

这里的问题是,在 if 语句中,您使用的 break 会打断流程,而您应该使用 continue

主要更改:(我添加了注释行以使更改对您来说更明显)

     int check = 1; //added a check variable
     while (check) { //used check variable for while loop
printf("\n Try another operation [y/n]: ");
            scanf("%c", &choice2); //changed %d to %c as the input is a char
            if (choice2 == 'y' || choice2 == 'Y') {
                printf("Retrying...");
                continue; } //changed break with continue
            else if (choice2 == 'n' || choice2 == 'N') {
                printf("Exiting...");
                check= 0; } //updated check so next iteration fails and terminates the loop
            else {
                printf("Error wrong input");
                check= 0; //there cannot be a 2nd try using if else statement
            }

there will be few more changes which I'll mark in the code below

#include <stdio.h>
int main() {
    int check = 1; //added a check variable
    while (check) { //used check variable for while loop
        int choice1, choice2, num1, num2;
        printf("\n  [1] Addition\n  [2] Subtraction\n  [3] Multiplication\n  [4] Division\n");
        printf("\n Pick a choice: ");
        scanf("%d", &choice1);
        printf("\n Give a number: ");
        scanf("%d", &num1);
        printf("\n Give another number: ");
        scanf("%d", &num2);
        switch (choice1) {
            case 1:
            printf("Sum is %d", num1+num2);
            break;
            case 2:
            printf("Difference is %d", num1-num2);
            break;
            case 3:
            printf("Product is %d", num1*num2);
            break;
            case 4:
            printf("Quotient is %d", num1/num2);
            break;
            default:
            printf("Please select a valid operation");
        }
            printf("\n Try another operation [y/n]: ");
            scanf("%c", &choice2); //changed %d to %c as the input is a char
            if (choice2 == 'y' || choice2 == 'Y') {
                printf("Retrying...");
                continue; } //changed break with continue
            else if (choice2 == 'n' || choice2 == 'N') {
                printf("Exiting...");
                check= 0; } //updated check so next iteration fails and terminates the loop
            else {
                printf("Error wrong input");
                check= 0; //there cannot be a 2nd try using if else statement
            }
    }
    return 0;
}

更多详情,您可以阅读docs

我认为您必须进行一些重大更改,如下所示

#include <stdio.h>
int main() {
    while (1) {
        int choice1, num1, num2;
        char choice2;
        printf("\n  [1] Addition\n  [2] Subtraction\n  [3] Multiplication\n  [4] Division\n");
        printf("\n Pick a choice: ");
        scanf("%d", &choice1);
        printf("\n Give a number: ");
        scanf("%d", &num1);
        printf("\n Give another number: ");
        scanf("%d", &num2);
        switch (choice1) {
            case 1:
            printf("Sum is %d", num1+num2);
            break;
            case 2:
            printf("Difference is %d", num1-num2);
            break;
            case 3:
            printf("Product is %d", num1*num2);
            break;
            case 4:
            printf("Quotient is %d", num1/num2);
            break;
            default:
            printf("Please select a valid operation");
        }
            printf("\n Try another operation [y/n]: ");
            scanf("%s", choice2);

            if (choice2 == 'y' || choice2 == 'Y')
            {
                scanf("%d", &choice1);
            }
            else;
            {
                printf("Exiting...");
            }

    }
    return 0;
}