0
晚上好,y/n循环结束功能
这是我的代码。我正在制作一个小型计算器,但是我最终还是在努力以y/n循环重复该函数。我看过别人,但似乎无法得到正确的答案。 谢谢。
#include <stdio.h>
int main()
{
int n, num1, num2, result;
char answer;
{
printf("\nWhat operation do you want to perform?\n");
printf("Press 1 for addition.\n");
printf("Press 2 for subtraction.\n");
printf("Press 3 for multiplication.\n");
printf("Press 4 for division.\n");
scanf("%d", &n);
printf("Please enter a number.\n");
scanf("%d", &num1);
printf("Please enter the second number.\n");
scanf("%d", &num2);
switch(n)
{
case 1: result = num1 + num2;
printf("The addition of the two numbers is %d\n", result);
break;
case 2: result = num1 - num2;
printf("The subtraction of the two numbers is %d\n", result);
break;
case 3: result = num1 * num2;
printf("The multiplication of the two numbers is %d\n", result);
break;
case 4: result = num1/num2;
printf("The division of the two numbers is %d\n", result);
break;
default: printf("Wrong input!!!");
}
printf("\nDo you want to continue, y/n?\n");
scanf("%c", &answer);
while(answer == 'y');
}
return 0;
}
您正在使用错误while循环。请看一些示例,然后你可以正确地做到这一点。 – Talal
将所有东西包装在后测循环中。 –
我想你想要一个['do/while'](https://www.tutorialspoint.com/cprogramming/c_do_while_loop.htm)循环。 –