在过去的3天里,我一直在尝试修复此代码。有人可以帮忙。如果插入负数,程序不会打印最后一个语句。如果输入负数,而不是打印“终止程序...”,它会在退出之前跳转到打印“输入第一个数字:”。
int main(void)
{
int choose, number1, number2, total_hold;
do
{
printf("\n");
printf("1: sum of two numbers \n");
printf("2: difference of two numbers\n");
printf("3: product of two numbers\n");
printf("<0:terminate the program\n");
printf("Select calculation:");
scanf("%d", &choose);
printf("Enter the first number:");
scanf("%d", &number1);
printf("Enter the second number:");
scanf("%d", &number2);
if (choose == 1)
{
total_hold = sumOfNumbers(number1, number2);
displayS(number1, number2, total_hold);
}
else if (choose == 2)
{
total_hold = differenceOfNumbers(number1, number2);
displayD(number1, number2, total_hold);
}
else if (choose == 3)
{
total_hold = MultiplicationOfNumbers(number1, number2);
displayM(number1, number2, total_hold);
}
else if (choose < 0)
{
printf("Terminating the program...");
break;
}
}
while (choose > 0);
return 0;
}
答案 0 :(得分:2)
放置这个:
if (choose < 0)
{
printf("Terminating the program...");
break;
}
紧接着scanf("%d", &choose)
。
除此之外,您也可以将while (choose > 0)
更改为while (1)
。
答案 1 :(得分:1)
您正在使用
do{
statement 1;
statement 2;
...
...
statement n;
}while(condition);
因此,在do-while循环中,所有语句至少执行一次,然后检查条件。
最好使用while循环而不是do-while
答案 2 :(得分:1)
您在任何if语句之前执行三条scanf指令。如果你想检查选择变量是否低于0你可以在其他两个scanf调用之前执行此操作
int main(void)
{
int choose, number1, number2, total_hold;
do
{
printf("\n");
printf("1: sum of two numbers \n");
printf("2: difference of two numbers\n");
printf("3: product of two numbers\n");
printf("<0:terminate the program\n");
printf("Select calculation:");
scanf("%d", &choose);
if (choose < 0)
{
printf("Terminating the program...");
break;
}
printf("Enter the first number:");
scanf("%d", &number1);
printf("Enter the second number:");
scanf("%d", &number2);
if (choose == 1)
{
total_hold = sumOfNumbers(number1, number2);
displayS(number1, number2, total_hold);
}
else if (choose == 2)
{
total_hold = differenceOfNumbers(number1, number2);
displayD(number1, number2, total_hold);
}
else if (choose == 3)
{
total_hold = MultiplicationOfNumbers(number1, number2);
displayM(number1, number2, total_hold);
}
}
while (choose > 0);
return 0;
}
答案 3 :(得分:1)
言
printf("Enter the first number:");
scanf("%d", &number1);
printf("Enter the second number:");
scanf("%d", &number2);
独立于可变choose
中输入的值执行,因为在这些语句之后对最后一个进行了测试。
程序的设计更正确(没有测试)
#include <stdio.h>
int main(void)
{
while ( 1 )
{
enum { Exit = 0, Addition = 1, Difference = 2, Multiplication = 3 };
unsigned int choose;
int number1, number2, total_hold;
printf( "\n" );
printf( "%d: sum of two numbers \n", Addition );
printf( "%d: difference of two numbers\n", Difference );
printf( "%d: product of two numbers\n", Multiplication );
printf( "%d: terminate the program\n", Exit );
printf( "Select calculation:" );
scanf( "%u", &choose );
if ( choose == Exit )
{
printf( "Terminating the program..." );
break;
}
if ( choose <= Multiplication )
{
printf( "Enter the first number:" );
scanf( "%d", &number1 );
printf( "Enter the second number:" );
scanf( "%d", &number2 );
}
switch ( choose )
{
case Addition: {
total_hold = sumOfNumbers(number1, number2);
displayS(number1, number2, total_hold);
break;
case Difference:
total_hold = differenceOfNumbers(number1, number2);
displayD(number1, number2, total_hold);
break;
case Multiplication:
total_hold = MultiplicationOfNumbers(number1, number2);
displayM(number1, number2, total_hold);
break;
default:
printf("Invalid selection. Try again.\n");
break;
}
}
return 0;
}