新手求问,关于if语句?
#include "stdio.h"#include <math.h>
int main()
{
int money ,n,p0,p;
float r,r0=0.0035;
printf("请输入您的存款金额\n");
scanf_s("%d", &money);
printf("请输入您的存款年份\n");
scanf_s("%d", &n);
if (n = 1)r = 0.015;
if (n = 2)r = 0.021;
if (n = 3)r = 0.0275;
if (n = 5)r = 0.03;
printf("您计划存定期还是活期还是年活?定期输入1,活期输入2,年活输入3\n");
scanf_s("%d", &p0);
if (p0 = 1)
{
p = 1000 * (1 + n * r);
printf("您的本息和是%d\n", p);
};
if (p0 = 2)
{
p = 1000 * pow((1 + r0), n);
printf("您的本息和是%d\n", p);
};
if (p0 = 3)
{
p = 1000 * pow((1 + (r = 0.015) / 4), 4 * n);
printf("您的本息和是%d\n", p);
};
return 0;
http://m.qpic.cn/psc?/V53hwHAI0fK09x3GaLrc1x2X6Q4RK3gQ/TmEUgtj9EK6.7V8ajmQrECDWGmHy0kuVuxYV*nb5IF1xinnNT6l6iReNhvkF9X.nA4rOv.Z9C7ZNBa*EbiS2oYbLhu7lZBwPrj*C*jDO9PA!/b&bo=4QMHAgAAAAADF9U!&rf=viewer_4
这是运行的结果,不是应该只出来一个利率么?为啥出来了三个? if (n = 1)r = 0.015;
if (n = 2)r = 0.021;
if (n = 3)r = 0.0275;
if (n = 5)r = 0.03;
. . . . . .
if (p0 = 1)
. . . . . .
if (p0 = 2)
. . . . . .
if (p0 = 3)
改为:
if (n == 1)r = 0.015;
if (n == 2)r = 0.021;
if (n == 3)r = 0.0275;
if (n == 5)r = 0.03;
. . . . . .
if (p0 == 1)
. . . . . .
if (p0 == 2)
. . . . . .
if (p0 == 3) =是赋值, ==才是相等。 你判断语句的问题,首先是“==”等于和“=’赋值的混淆。
这两个符号代表的含义不同,一定要注意。
然后是if只要一个,下面两个换成else if
(用if也是可以的)
页:
[1]