gpt4 book ai didi

C 新手/不明白问题可能是什么

转载 作者:行者123 更新时间:2023-11-30 21:26:52 27 4
gpt4 key购买 nike

自从我学习 C 语言以来,我决定制作一个简单的程序,用于加、减和计算两个变量的乘积。根据用户的输入是1,2还是3来选择加/减/折叠。

#include <stdio.h>

int main (void) {
int a, b, c, d, e, f, g;
a=19; b=11; c=a+b; d=a-b; e=a*b; f=-1;

while (f<0 && f>3) {
printf("-press 1 to calculate the sum of a and b\n");
printf("-press 2 to calculate the difference between a and b\n");
printf("-press 3 to calculate the product of a and b\n");

scanf("%d\n",&g);
f=g;
return;
}

if (f == 1) {
printf("A+B= %ls\n", &c);
} else if (f == 2) {
printf("A-B= %ls\n", &d);
} else if (f == 3) {
printf(" A*B= %ls\n", &e);
}
return 0;
}

When i run the program its reads "g" and then it stops. any suggestions to why is this happening btw i also tried removing the while statement.

最佳答案

我认为你的意思是下面的 while 循环

while ( f < 0 || f > 3 )
{
printf("-press 1 to calculate the sum of a and b\n");
printf("-press 2 to calculate the difference between a and b\n");
printf("-press 3 to calculate the product of a and b\n");
scanf( "%d", &g );
f = g;
}

即注意1)while循环的条件,2)删除return语句,3)删除scanf中的字符'\n',因为它是多余的。

并且在 printf 调用中删除运算符 & 并使用转换说明符 %d。

 if ( f == 1 ) {
printf(" A+B= %d\n", c) ;
}
else if ( f == 2) {
printf(" A-B= %d\n", d );
}
else if ( f == 3 ) {
printf(" A*B= %d\n",e );
}

关于C 新手/不明白问题可能是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58390689/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com