注册 登录
编程论坛 C语言论坛

请问这样写for错在哪里?

清风幽然 发布于 2019-09-25 15:37, 2038 次点击
22. Write a program which requests a user to enter an amount of money. The program
prints the interest payable per year for rates of interest from 5% to 12% in steps
of 0.5%.

#include <stdio.h>
int main (){
 int y=0;
 double m,r,i;
 printf ("enter the amount of money : ");
 scanf ("%lf",&m);
 printf ("YEAR  INTEREST\n\n");
 for (r=0.05; r<=0.12; r+=0.005; y++)
 i=m*r;
 printf ("\n%d %6.2f",y,i);
}


[此贴子已经被作者于2019-9-25 17:55编辑过]

6 回复
#2
rjsp2019-09-25 16:05
程序代码:
#include <stdio.h>

int main( void )
{
    double money;
    printf( "enter the amount of money: " );
    if( scanf("%lf",&money)!=1 || money<0 )
        return 1;

    //  from 5% to 12% in steps of 0.5%
    printf( "RATE\tINTEREST\n");
    for( unsigned rate=50; rate<=120; rate+=5 )
    {
        double interest = money * rate/1000.;
        printf( "%4.1f%%\t%6.2f\n", rate/10., interest );
    }
}

输入
1000
输出
RATE    INTEREST
 5.0%    50.00
 5.5%    55.00
 6.0%    60.00
 6.5%    65.00
 7.0%    70.00
 7.5%    75.00
 8.0%    80.00
 8.5%    85.00
 9.0%    90.00
 9.5%    95.00
10.0%   100.00
10.5%   105.00
11.0%   110.00
11.5%   115.00
12.0%   120.00


#3
wlcsss1232019-09-25 17:07
marn ??
#4
清风幽然2019-09-25 17:54
回复 2楼 rjsp
Thank you !

But how can I add the year++ in the for loop to calculate year ?
#5
niuniuchiniu2019-09-25 23:21
printf ("enter the amount of money : "); 完完全全的中式英文.
#6
bcbbcclbbc2019-09-26 01:55
很明显的for语句括号里可以有三个语句(语句1;语句2;语句3)并用分号隔开,而楼主多写了一个分号
for (r=0.05; r<=0.12; r+=0.005;y++)

可以将最后一个分号改成逗号,形成一个逗号表达式
for(r=0.05;r<=0.12;r+=0.005,y++)
#7
清风幽然2019-09-26 14:34
回复 6楼 bcbbcclbbc
Thank you !
1