2011-03-15 53 views
0

我构建了一个运行着一个java类的计算器。我遇到了一个障碍。我有完整的计算器内置和编码在Microsoft Word(功能等)。构建一个循环

我需要添加到它,用循环;我离开的地方跑完美。现在我需要继续它,每次我进入它说,“不要把它放在这里”。如果是这样的话,我还有多远?或者我需要在计算器上添加什么?

请指教,如果我问这个权利与否。

下面是一些计算器:

public static void main(String[] args) { 

     //Declaring and initializing the variables 
     double principle = 200000.0; 
     double interestRate = 0.0575; 
     int term = 360; 

     DecimalFormat decimalPlaces=new DecimalFormat("0.00"); 

     // Calculating the monthly payment: M = P [ i(1 + i)n ]/[ (1 + i)n - 1] 
     double monthlyRate = (interestRate/12); 
     double t = Math.pow((1 + monthlyRate),term); 
     double payment = (principle * monthlyRate * t)/(t-1); 

     //Display the results on computer screen 
     System.out.println("The monthly payment for a mortgage of 200000 is $" + 
      decimalPlaces.format(payment)); 

这是我想补充:

#include <iostream> 
#include "math.h" 

using namespace std; 

double calcPayment(double principle, double rate, double term) { 
+1

@buddy:显示代码。 – 2011-03-15 23:22:50

+8

“建立在微软的话”与Java ...? – 2011-03-15 23:23:05

+1

不幸的是,你问这个问题的方式很不明确。请记住,您需要假设我们对您的程序设置方式一无所知。请编辑您的问题尽可能清晰和详细,并在适用的情况下包含代码示例。另外,还可以使用正确的标点符号并减少运行的句子。 – StriplingWarrior 2011-03-15 23:24:16

回答

0

首先,你应该在你的算法方面改变,因为这应该是在几年而不是几天。关于到Mortgage Payment Calculator你的方法来计算应该是这样的:

public static double calculateMonthlyPayment(double principle, double rate, int termsInYears) { 
    double mRate = rate/12/100; 
    int months = termsInYears * 12; 
    double pow = Math.pow((1 + mRate), months); 

    return (1.0 - 1.0/(1 - pow)) * mRate * principle; 
} 

public static void main(String[] args) throws InterruptedException { 
    // Declaring and initializing the variables 
    double principle = 200000.0; 
    double interestRate = 0.0575; 
    int terms = 12; 

    DecimalFormat decimalPlaces = new DecimalFormat("0.00"); 
    System.out.println("The monthly payment for a mortgage of 200000 is $" 
      + decimalPlaces.format(calculateMonthlyPayment(principle, interestRate, terms))); 
} 

不过说真的我猜,如果这是你想要的 - 我不知道为什么你要使用循环

0
#include <iostream> 
#include "math.h" 

using namespace std; 

这不是java。它看起来像你复制了一些C++代码,并试图将它添加到你的java类。