从我的方法打印问题

时间:2011-06-12 21:28:42

标签: java methods

我是Java的新手,我一直在努力让它发挥作用。 我一直试图让这个打印方法在最后几个小时工作,但我无法弄清楚它有什么问题。我运行时没有任何错误。我只想在完成所有计算后显示一个输出框。 我需要询问用户以下信息并显示以下输出: 要比较的贷款数量 房价 预付款

我的问题:我的数学有问题,输出框不止一次显示。

我非常感谢我能得到的任何帮助。在此先感谢!

    package javamortgagecalculator;
    import javax.swing.JOptionPane;
    public class JavaMortgageCalculator {

    public static void main(String[] args) {
    int numberOfLoans;
    double sellingPrice;
    double downPayment;
    double loanAmount;
    double annualInterestRate = 0.0;
    int numberOfYears = 0;
    double[] interestRatesArr;
    int[] numberOfYearsArr;
    double[] monthlyPayment;

    //A. Enter the Number Of Loans to compare and Convert numberOfLoansString to int
    String numberOfLoansString = JOptionPane.showInputDialog("Enter the Number Of Loans to Compare");
    numberOfLoans = Integer.parseInt(numberOfLoansString);

    //B. Enter the Selling Price of Home Convert homeCostString to double
    String sellingPriceString = JOptionPane.showInputDialog("Enter the Loan Amount");
    sellingPrice = Double.parseDouble(sellingPriceString);

    //C. Enter the Down Payment on the Home
    String downPaymentString = JOptionPane.showInputDialog("Enter the down payment on the Home");
    downPayment = Double.parseDouble(downPaymentString);

    //Get the loanAmount by Subtracting the Down Payment from homeCost
    loanAmount = sellingPrice - downPayment;

    interestRatesArr = new double[numberOfLoans];
    numberOfYearsArr = new int[numberOfLoans];
    monthlyPayment = new double[numberOfLoans];
    int counter = 1;

    for (int i = 0; i < numberOfLoans; i++) {
        //Enter the Interest Rate
        String annualInterestRateString = JOptionPane.showInputDialog("Enter the interest rate for Scenario " + counter);
        annualInterestRate = Double.parseDouble(annualInterestRateString);
        interestRatesArr[i] = (annualInterestRate);

        //D2 Get the number of years
        String numberOfYearsString = JOptionPane.showInputDialog("Enter the number of years for Scenario " + counter);
        numberOfYears = Integer.parseInt(numberOfYearsString);
        numberOfYearsArr[i] = (numberOfYears);

        counter++;
    }

    printArray(numberOfLoans,
    sellingPrice,
    downPayment,
    loanAmount,
    annualInterestRate,
    numberOfYears,
    interestRatesArr,
    numberOfYearsArr,
    monthlyPayment);

}


//public static void printArray(int numOfLoans, double price, double dwnPayment, double loanAmt, double[] printRate, int[] printYears) {

  public static void printArray(int numberOfLoans2,
    double sellingPrice2,
    double downPayment2,
    double loanAmount2,
    double annualInterestRate2,
    int numberOfYears2,
    double[] interestRatesArr2,
    int[] numberOfYearsArr2,
    double[] monthlyPayment2){


    for (int i = 0; i < numberOfLoans2; i++) {
        //Calculate monthly payment
        double monthlyPayment = loanAmount2 * annualInterestRate2 / (1 - 1 / Math.pow(1 + annualInterestRate2, numberOfYears2 * 12));
        //Format to keep monthlyPayment two digits after the decimal point
        monthlyPayment = (int) (monthlyPayment * 100) / 100.0;
        //Store monthlyPayment values in an array
        monthlyPayment2[i] = (monthlyPayment);

        //Calculate total Payment
        double totalPayment = monthlyPayment2[i] * numberOfYears2 * 12;

        //Format to keep totalPayment two digits after the decimal point
        //totalPayment = (int) (totalPayment * 100) / 100.0;
        //totalPaymentArray[i] = (totalPayment);

        StringBuilder sb = new StringBuilder();
        int n = 0;
        for (int x = 0; x < numberOfLoans2; x++) {
            if (n == 0) {
                sb.append(String.format("%s\t\t %s\t\t %s\t\t %s\t\t %s\t\t %s\t\t %n", "Selling Price", "Down Payment", "Loan Amount", "Rate", "Years", "Payment"));
            }
            sb.append(String.format("%s\t\t\t\t %s\t\t\t\t\t\t %s\t\t\t\t\t\t\t\t %s\t\t\t\t\t\t\t\t %s\t\t\t\t\t\t %s\t\t\t\t %n", "$" + sellingPrice2, "$" + downPayment2, "$" + loanAmount2, interestRatesArr2[i] + "%", numberOfYearsArr2[i], "$" + monthlyPayment2[i]));
            n++;
        }

        String toDisplay = sb.toString();

        JOptionPane.showMessageDialog(null, sb.toString(), toDisplay, JOptionPane.INFORMATION_MESSAGE);
    }

}

}

0 个答案:

没有答案