如果在While循环中有其他

时间:2015-10-02 21:03:00

标签: c

我遇到了这段代码的问题。 当我运行它时,它只在while语句中打印出两行。 我试图让它运行if语句直到MortgageLeft

有人能看到我犯的错误吗? Idk如果这有帮助,但如果我删除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;
}

4 个答案:

答案 0 :(得分:2)

看来你缺少一些括号

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;
        }
   }

答案 1 :(得分:0)

在else语句中,您有这一行MortgageLeft=0;。这就是你的while循环不断退出的原因。此外,在您发布的代码中,您似乎忽略了else语句周围的花括号。

答案 2 :(得分:0)

#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;
        }

}

答案 3 :(得分: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; // only this statement is 
                                                               // part of the else branch
  Month++;
  printf("%d\t\t %.2f\t 0", Month, MortgageLeft);
  MortgageLeft = 0;
}

你需要围绕所有 else分支中的语句括号:

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;
  }
}

编译器不关心您如何格式化代码,但它可以在发现错误(由您或由审阅者)中产生巨大的差异。空白是你的朋友;不要害怕在表达式之间和代码行之间放置一些喘息空间。 25年后你的眼睛会感谢你。它确实让您和其他人更容易阅读,它可以帮助您更轻松地发现这样的问题。

IME,直接用控制结构的开头括起来使这些结构更容易理解。只需2到4个压痕空间即可清楚地显示范围。

修改

事实证明,其他人编辑了原始代码以便于阅读。