2013-10-31 64 views
1

我對編程非常陌生,在主要方法中顯示變量monthlyPayment時遇到了一些問題;我認爲這與以前的方法有關。這是一個每月付款計算器。無法顯示變量

import java.util.Scanner; 
public class assignment8 { 

public static double pow(double a, int b) { 
    double ans = 1; 
    if (b < 0) { 
     for (int i = 0; i < -b; i++) { 
      ans *= 1/a; 
     } 
    } 
    return ans; 
} 

public static double monthlyPayment(double amountBorrowed, int loanLength, int percentage) { 
    double monthlyPayment; 
    double P = amountBorrowed; 
    double N = 12 * loanLength; 
    double r = (percentage/100)/12; 
    monthlyPayment = (r * P)/(1 - Math.pow((1 + r) , -N)); 
    return monthlyPayment; 
} 

public static void main(String[] args) { 
    Scanner kbd = new Scanner(System.in); 

    System.out.print("Enter the amount borrowed: $"); 
    double amountBorrowed = kbd.nextDouble(); 

    System.out.print("Enter the interest rate: "); 
    int interestRate = kbd.nextInt(); 

    System.out.print("Enter the minimum length of the loan: "); 
    int minLoanLength = kbd.nextInt(); 

    System.out.print("Enter the maximum length of the loan: "); 
    int maxLoanLength = kbd.nextInt(); 

    while (maxLoanLength < minLoanLength) { 
     System.out.print("Enter the maximum legth og the loan: "); 
     maxLoanLength = kbd.nextInt(); 
    } 
    for (int i = minLoanLength; i <= maxLoanLength; i++) { 

     System.out.println(i + monthlyPayment); 
    } 
} 
} 
+0

'顯示變量有些麻煩' - 它有什麼樣的麻煩?你可以再詳細一點嗎? – sandrstar

回答

2

這是您的monthlyPayment方法:

public static double monthlyPayment(double amountBorrowed, int loanLength, int percentage) 

它需要3個參數,並返回一個double。

這是你如何調用你monthlyPayment方法:

System.out.println(i + monthlyPayment); 

你不發送的任何參數。你甚至不包括()。你的編譯器應該抱怨。

您需要而不是這樣做:

System.out.println(i + monthlyPayment(amountBorrowed, loanLength, percentage)); 

注意:你還是可能不會得到你期望的結果。這會將i和您致電monthlyPayment的結果相加並打印出來。你可能想是這樣的:

System.out.println("Month " + i + " payment: " + monthlyPayment(amountBorrowed, loanLength, percentage)); 
+0

謝謝!我仍然遇到一些錯誤,但我認爲我可以解決這些問題。 – user2939707

+0

我很想知道我爲什麼被低估。 – nhgrif

+0

我不認爲這是我,是嗎? – user2939707

2
monthlyPayment(double amountBorrowed, int loanLength, int percentage) 

你需要傳遞參數

System.out.println(i + monthlyPayment(amountBorrowed, loanLength, percentage)); 
1

試試這個

System.out.println(i + ": " + monthlyPayment(amountBorrowed, loanLength, percentage)); 

類型的imonthlyPayment是int和double。默認情況下,2號碼的+運營商將返回2號碼的總和。

在使用+之前,您需要將數字轉換爲字符串。