有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java构建循环

我用java类构建了一个运行计算器。我遇到了困难。我有一个完整的计算器,它是在微软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) {

共 (2) 个答案

  1. # 1 楼答案

    #include <iostream>
    #include "math.h"
    
    using namespace std;
    

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

  2. # 2 楼答案

    首先,你应该改变你的算法,因为这应该是几年而不是几天。关于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)));
    }
    

    但我真的在猜测这是否是你想要的——我不知道你为什么想要使用循环