[英]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);
}
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.