是/否循环在C中使用while循环与数学运算程序



首先,我对C(和任何编程)和堆栈溢出都是一个初学者,所以如果以前有人问过这样的问题,我很抱歉。

所以我在循环部分的数学运算代码中遇到了麻烦。当我输入N或Y时,程序会认为我输入了不同的选项,并告诉我必须输入Y或N。

下面是代码。对不起,如果这是一个混乱的混乱,这是我所知道的到目前为止。

#include <stdio.h>
int main() {
while (1) {
int choice1, choice2, num1, num2;
printf("n  [1] Additionn  [2] Subtractionn  [3] Multiplicationn  [4] Divisionn");
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语句中,您使用的是中断中断流,而应该使用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
}

我将在下面的代码中标记更多的更改

#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] Additionn  [2] Subtractionn  [3] Multiplicationn  [4] Divisionn");
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;
}

要了解更多细节,您可以阅读文档

我认为你必须做一些重大的改变,如下所示

#include <stdio.h>
int main() {
while (1) {
int choice1, num1, num2;
char choice2;
printf("n  [1] Additionn  [2] Subtractionn  [3] Multiplicationn  [4] Divisionn");
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;
}

最新更新