簡體   English   中英

多條 Java 輸入

[英]More Than One Java Input

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

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

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

您需要在for(... )循環等循環內完成所有重復的處理邏輯。 使用數組存儲貸款數量的不同值。

為此使用for 循環

PS:您也可以使用其他循環 [while, do-while]。

使用 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
}

您可能需要使用 arrays 和循環。 使用 arrays 存儲所有輸入的值並使用循環獲取值。

   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 個 arrays 值。 您可以使用第二個循環來計算 output 並顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM