我不明白如何显示我的计算

时间:2015-04-09 22:44:10

标签: java

该程序允许用户输入贷款金额和贷款期限。

每月和总付款应以利率增加1/8显示。到目前为止,我已经足够正确地计算了一个数量,但我不明白如何显示所有结果。

我拍下了下面问题的说明:

http://imgur.com/s9JEbtu

import java.util.Scanner;

class ComputeLoan

{
public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);

    //Enter number of years
    System.out.print("Enter number of years as an integer: ");
        int numberOfYears = input.nextInt();

    //Enter loan amount
    System.out.print("Enter Loan amount: ");
    double loanAmount = input.nextDouble();

    //Declare interest rate
    double interestRate = 5.000;

    //Obtain monthly interest rate
    double monthlyInterestRate = interestRate / 1200;

    //Calculate payment

    double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 /                                                              Math.pow(1 + monthlyInterestRate, numberOfYears * 12));

double totalPayment = monthlyPayment * numberOfYears * 12;

    for (double intRate=5.000; intRate <8.000; intRate = intRate + .125);
    {
    double intRate=5.000;
    System.out.println("The InterestRate is " + intRate);
    System.out.println("The monthly payment is $" +
        (int)(monthlyPayment * 100) / 100.0);
    System.out.println("The total payment is $" +
        (int)(totalPayment * 100) / 100.0);


} 
    //Display Results





}
}

2 个答案:

答案 0 :(得分:0)

你好试试像这样计算for循环

import java.util.Scanner;

class ComputeLoan

{
public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);

    //Enter number of years
    System.out.print("Enter number of years as an integer: ");
        int numberOfYears = input.nextInt();

    //Enter loan amount
    System.out.print("Enter Loan amount: ");
    double loanAmount = input.nextDouble();


    for (double intRate=5.000; intRate <8.000; intRate = intRate + .125)
    {

    //Obtain monthly interest rate
    double monthlyInterestRate = intRate / 1200;

    //Calculate payment

    double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 /                                                              Math.pow(1 + monthlyInterestRate, numberOfYears * 12));

double totalPayment = monthlyPayment * numberOfYears * 12;
    System.out.println("The InterestRate is " + intRate);
    System.out.println("The monthly payment is $" +
        (int)(monthlyPayment * 100) / 100.0);
    System.out.println("The total payment is $" +
        (int)(totalPayment * 100) / 100.0);


} 
}
}

输出

Enter number of years as an integer: 5
Enter Loan amount: 10000
The InterestRate is 5.0
The monthly payment is $188.71
The total payment is $11322.74
The InterestRate is 5.125
The monthly payment is $189.28
The total payment is $11357.13
The InterestRate is 5.25
The monthly payment is $189.85
The total payment is $11391.59
The InterestRate is 5.375
The monthly payment is $190.43
The total payment is $11426.11
The InterestRate is 5.5
The monthly payment is $191.01
The total payment is $11460.69
The InterestRate is 5.625
The monthly payment is $191.58
The total payment is $11495.34
The InterestRate is 5.75
The monthly payment is $192.16
The total payment is $11530.06
The InterestRate is 5.875
The monthly payment is $192.74
The total payment is $11564.83
The InterestRate is 6.0
The monthly payment is $193.32
The total payment is $11599.68
The InterestRate is 6.125
The monthly payment is $193.9
The total payment is $11634.58
The InterestRate is 6.25
The monthly payment is $194.49
The total payment is $11669.55
The InterestRate is 6.375
The monthly payment is $195.07
The total payment is $11704.59
The InterestRate is 6.5
The monthly payment is $195.66
The total payment is $11739.68
The InterestRate is 6.625
The monthly payment is $196.24
The total payment is $11774.85
The InterestRate is 6.75
The monthly payment is $196.83
The total payment is $11810.07
The InterestRate is 6.875
The monthly payment is $197.42
The total payment is $11845.36
The InterestRate is 7.0
The monthly payment is $198.01
The total payment is $11880.71
The InterestRate is 7.125
The monthly payment is $198.6
The total payment is $11916.13
The InterestRate is 7.25
The monthly payment is $199.19
The total payment is $11951.61
The InterestRate is 7.375
The monthly payment is $199.78
The total payment is $11987.16
The InterestRate is 7.5
The monthly payment is $200.37
The total payment is $12022.76
The InterestRate is 7.625
The monthly payment is $200.97
The total payment is $12058.44
The InterestRate is 7.75
The monthly payment is $201.56
The total payment is $12094.17
The InterestRate is 7.875
The monthly payment is $202.16
The total payment is $12129.97

答案 1 :(得分:0)

查看您链接到我们的图片,我猜测您正在尝试为每列提供相等的空间。 System.out.printf()String.format()是您可能感兴趣的功能。

下面的代码将第一个十进制数转换为3个位置,第二个和第三个转换为2个位置。每列包含20个空格。

    System.out.printf("%20s%20s%20s", 
            "Interest Rate", "Monthly Payment", "Total Payment");
    System.out.printf(%20.3f%20.2f%20.2f,
            intRate, monthlyPayment, totalPayment);