gpt4 book ai didi

c - If Else 在 While 循环中

转载 作者:行者123 更新时间:2023-11-30 21:20:55 24 4
gpt4 key购买 nike

我在使用这段代码时遇到了问题。当我运行它时,它只打印出 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/

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