2011-06-10 43 views
0

我是這個論壇和Java的新手。我很難嘗試找到一種方法來要求用戶輸入多個貸款,以便從步驟D中進行比較。我需要能夠向用戶詢問他們在步驟A中輸入的金額的不同利率和年數。因此,如果他們輸入了10,那麼我將不得不問他們10次的利率和年數並輸出它以使用選項卡的表格格式。任何幫助深表感謝。提前致謝。超過一個Java輸入

編輯:非常感謝您的幫助!我更新了代碼。

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

    //B. Enter the Amount/Selling Price of Home 
    String loanAmountString = JOptionPane.showInputDialog("Enter the loan amount:"); 
    //Convert loanAmountString to double 
    double loanAmount = Double.parseDouble(loanAmountString); 

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


    //D. Ask the following for as many number of loans they wish to compare 
    //D1 Get the interest rate 
    double[] anualInterestRatesArray = new double[numberOfLoans]; 
    double[] monthlyInterestRateArray = new double[numberOfLoans]; 
    int[] numberOfYearsArray = new int[numberOfLoans]; 
    double[] monthlyPaymentArray = new double[numberOfLoans]; 
    double[] totalPaymentArray = new double[numberOfLoans]; 

    for (int i=0; i < numberOfLoans; i++) 
    { 
     String annualInterestRateString = JOptionPane.showInputDialog("Enter the interest rate:"); 
     double annualInterestRate = Double.parseDouble(annualInterestRateString); 
     anualInterestRatesArray[i] = (annualInterestRate); 

     //Obtain monthly interest rate 
     double monthlyInterestRate = annualInterestRate/1200; 
     monthlyInterestRateArray[i] = (monthlyInterestRate); 

     //D2 Get the number of years 
     String numberOfYearsString = JOptionPane.showInputDialog("Enter the number of years:"); 
     int numberOfYears = Integer.parseInt(numberOfYearsString); 
     numberOfYearsArray[i] = (numberOfYears); 

     //Calculate monthly payment 
     double monthlyPayment = loanAmount * monthlyInterestRate/(1 - 1/Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); 
     //Format to keep monthlyPayment two digits after the decimal point 
     monthlyPayment = (int)(monthlyPayment * 100)/100.0; 
     //Store monthlyPayment values in an array 
     monthlyPaymentArray[i] = (monthlyPayment); 

     //Calculate total Payment 
     double totalPayment = monthlyPaymentArray[i] * numberOfYears * 12; 
     //Format to keep totalPayment two digits after the decimal point 
     totalPayment = (int)(totalPayment * 100)/100.0; 
     totalPaymentArray[i] = (totalPayment); 
    } 
+3

爲什麼你不使用循環?你想一遍又一遍地運行相同的代碼嗎? – 2011-06-10 17:39:45

+0

您有問題嗎? – 2011-06-10 17:59:36

+0

謝謝大家的幫助!我非常感謝!我更新了代碼。它對你們有什麼看法?如果它看起來很好,我只需要輸出結果。我如何從For循環中調用數組值? – PittsburghCoder 2011-06-10 19:07:44

回答

2

你需要做一個循環,如for(...)環路內的所有重複處理邏輯。使用數組來存儲貸款數量的不同值。

+0

非常感謝你們。你能給我一個你會做什麼的例子嗎?我一直在研究如何做到這一點,但我一直在做錯。非常感謝! – PittsburghCoder 2011-06-10 17:42:58

1

爲此使用for loops

P.S:您可以使用其他循環[一會兒,做,而作爲好。

0

使用for循環

int numberOfLoans = Integer.parseInt(numberOfLoansString); 

//section of code which shouldnt be repeated here outside the loop. 

for(int i = 0; i < numberOfLoans ; i++) 
{ 
     //Write Step D here , because you want it to be repeated 
} 
0

你可能需要使用數組和循環的一個例子。使用數組來存儲輸入的所有值,並使用循環來獲取值。

double[] anualInterestRates = new double[numberOfLoans]; 
    double[] monthlyInterestRates = new double[numberOfLoans]; 
    int[] numberOfyears = new int[numberOfLoans]; 

然後你可以循環,並要求每個貸款值:

for(int i= 0; i < numberOfLoans; i++){ 
     //get the anual interest rate 
     anualInterestRates[i] = the anual interets rate gotten 
     //etc 
    } 

現在你有值的3個陣列。您可以使用第二個循環來計算輸出和顯示。