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)循環。 –