2011-06-10 7 views
0

나는이 포럼과 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); 
    } 
+3

왜 루프를 사용하고 있지 않습니까? 똑같은 코드를 계속해서 반복해서 실행하고 싶습니까? –

+0

질문이 있으십니까? –

+0

모두 도와 주셔서 감사합니다! 나는 그것을 매우 감사한다! 코드를 업데이트했습니다. 너희들에게 어떻게 보이 나? 외모가 좋으면 결과물을 출력해야합니다. For 루프 내에서 배열 값을 어떻게 호출합니까? – PittsburghCoder

답변

2

당신은 루프와 같은 for(...) 루프 내에서 모든 반복 처리 로직을 수행해야합니다. 어레이를 사용하여 대출 수에 대한 다른 값을 저장하십시오.

+0

고마워요. 당신이 할 일에 대한 모범을 보여 주시겠습니까? 나는이 일을하는 방법을보고 있었지만 계속 잘못하고있다. 정말 고마워! – PittsburghCoder

1

이 경우 for loops을 사용하십시오.

P.S : [while, do-while] 다른 루프도 사용할 수 있습니다.

0

루프
당신은 아마 배열과 루프를 사용할 필요가

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 개 배열을 가지고있다. 두 번째 루프를 사용하여 출력 및 표시를 계산할 수 있습니다.

관련 문제