作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用这段代码时遇到了问题。当我运行它时,它只打印出 while 语句中的两行。我试图让它运行 if 语句,直到 MortgageLeft
有人可以看到我犯的错误吗?我不知道这是否有帮助,但如果我删除 else 语句,程序运行正常
#include <stdio.h>
#include <stdlib.h>
int main(){
float MortgageLeft, InterestRate, MonthlyPayment, MonIntRate, AmountOwed;
int Month=0;
printf("What is the value left on the mortgage?\n");
scanf("%f", &MortgageLeft);
printf("What is the annual interest rate of the loan, in percent?\n");
scanf("%f", &InterestRate);
printf("What is the monthly payment?\n");
scanf("%f", &MonthlyPayment);
MonIntRate= (InterestRate/12)/100;
printf("Month\t\t Payment\t Amount Owed\n");
while (MortgageLeft>0)
{
if(MortgageLeft>MonthlyPayment)
{
MortgageLeft=(MortgageLeft*MonIntRate)+MortgageLeft;
MortgageLeft=MortgageLeft-MonthlyPayment;
Month++;
printf("%d\t\t %.2f\t\t %.2f\n", Month, MonthlyPayment, MortgageLeft);
}
else
MortgageLeft=(MortgageLeft*MonIntRate)+MortgageLeft;
Month++;
printf("%d\t\t %.2f\t 0", Month, MortgageLeft);
MortgageLeft=0;
}
return 0;
}
最佳答案
您似乎缺少一些括号
while (MortgageLeft>0)
{
if(MortgageLeft>MonthlyPayment)
{
MortgageLeft=(MortgageLeft*MonIntRate)+MortgageLeft;
MortgageLeft=MortgageLeft-MonthlyPayment;
Month++;
printf("%d\t\t %.2f\t\t %.2f\n", Month, MonthlyPayment, MortgageLeft);
}
else
{
MortgageLeft=(MortgageLeft*MonIntRate)+MortgageLeft;
Month++;
printf("%d\t\t %.2f\t 0", Month, MortgageLeft);
MortgageLeft=0;
}
}
关于c - If Else 在 While 循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32915817/
我是一名优秀的程序员,十分优秀!