简体   繁体   English

多条 Java 输入

[英]More Than One Java Input

I am sort of a Newbie to this forum and to Java.我是这个论坛和 Java 的新手。 I am having a difficult time trying to find a way to ask the user to enter more than one loan to compare from steps D down.我很难找到一种方法来要求用户输入多个贷款以从步骤 D 向下进行比较。 I need to be able to ask the user for a different interest rate and number of years for the amount they entered in step A. So if they entered 10 then I would have to ask them 10 times for an interest rate and years and output it in a table format using tabs.我需要能够向用户询问他们在步骤 A 中输入的金额的不同利率和年数。因此,如果他们输入 10,那么我将不得不向他们询问 10 次利率和年数以及 output使用制表符的表格格式。 Any help is much appreciated.任何帮助深表感谢。 Thanks in advance.提前致谢。

Edit: Thank you so much for your help.编辑:非常感谢您的帮助。 I updated the code.我更新了代码。

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

You need to do all the repeated processing logic inside a loop such as for(... ) loop.您需要在for(... )循环等循环内完成所有重复的处理逻辑。 Use an array to store different values for the number of loans.使用数组存储贷款数量的不同值。

Use for loops for this.为此使用for 循环

PS: You can use other loops [while, do-while] as well. PS:您也可以使用其他循环 [while, do-while]。

An example for using for loop使用 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
}

You probably need to use arrays and loops.您可能需要使用 arrays 和循环。 Use arrays to store all the values entered and a loop to get the values.使用 arrays 存储所有输入的值并使用循环获取值。

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

Then you can loop and ask for each loan values:然后您可以循环并询问每个贷款值:

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

Now you have 3 arrays of values.现在您有 3 个 arrays 值。 You can use a second loop to calculate the output and display.您可以使用第二个循环来计算 output 并显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:使用BufferedReader输入多个空格 - java: taking input with BufferedReader- more than one spaces 如果元素多于一个,则循环在列表中找不到用户输入的字符串(Java) - Loop does not find user input String in List if there are more than one elements (Java) 如何在循环中的一个字符串中存储多个字符串变量输入(JAVA) - how to store more than string variable input in one string from loop (JAVA) 如何使用多个输入和 output 参数调用 postgresql function? - How to call postgresql function with more than one input and output parameter from Java? 通过使用Java Server Faces将多个输入字段绑定到支持bean属性? - Binding more than one input field to a backing bean property by using Java Server Faces? Java中有多个具有相同ResultSet的jTable - More than one jTable with the same ResultSet in Java Java / starts有多个字母吗? - Java / startsWith more than one letter? 将多个Java对象导入一个类? - Importing more than one Java object into a class? 类扩展多个Java类? - Class extending more than one class Java? 如何在Java中接收多个向量? - How to receive more than one vector in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM