多个Java输入

时间:2011-06-10 17:35:47

标签: java swing input

我是这个论坛和Java的新手。我很难找到一种方法来要求用户输入多个贷款来比较D步骤。我需要能够向用户询问他们在步骤A中输入的金额的不同利率和年数。因此,如果他们输入10,那么我将不得不问他们10次利率和年数并输出它使用制表符的表格格式。任何帮助深表感谢。提前谢谢。

编辑:非常感谢你的帮助!我更新了代码。

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

4 个答案:

答案 0 :(得分:2)

您需要在循环内执行所有重复处理逻辑,例如for( ... )循环。使用数组存储贷款数量的不同值。

答案 1 :(得分:1)

使用for loops

P.S:您也可以使用其他循环[while,do-while]。

答案 2 :(得分: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
}

答案 3 :(得分: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个值数组。您可以使用第二个循环来计算输出和显示。