如何将方法调用到我的main方法中以便它可以执行程序?

时间:2016-02-24 02:57:57

标签: java methods static-methods

显然,我不是在寻找所有的答案,但我会非常感谢你的一些方向。

所以我有这个程序,提示用户输入他们的年利率和储蓄金额。该程序计算6个月的复合值。

我遇到了将新方法调用到main方法来执行计算的问题。主要方法询问用户的年利率和储蓄金额。它调用新方法,如果所有都是正数,则执行程序。如果有任何消极,则会出现错误。

我以为我在调用它的最后一行调用了该方法,但显然我错了。我尝试使用谷歌搜索,并且无法理解这个调用方面。任何信息,将不胜感激。我很新,所以我还在编写这个编程的东西!不知道为什么最后一个大括号不在代码中。这是代码:

import java.text.DecimalFormat;

import javax.swing.JOptionPane;

public class FifthAssignment {

static double savingAmount = 0.0; // the 2 given static doubles
static double annualInterestRate = 0.0;
int sixthMonth;

public static double compoundValueMethod(double savingAmount, double annualInterestRate, int sixthMonth) {

    return sixthMonth; // sixMonth is the compound value
}

{
    DecimalFormat formatter = new DecimalFormat(".00");

    double monthlyInterestRate = annualInterestRate / 12;

    if (savingAmount < 0)
        if (annualInterestRate < 0) {
            JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
            System.exit(0);
        }

    if (savingAmount < 0) {
        JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
    }

    else if (annualInterestRate < 0) {
        JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
    }

    else {
        for (int i = 1; i <= 6; i++) {
            sixthMonth = (int) ((savingAmount + sixthMonth) * (1 + monthlyInterestRate));
        }

    }
}

public static void main(String[] args) {

    DecimalFormat formatter = new DecimalFormat(".00"); // bring in the
                                                        // decimal
                                                        // formatting for
                                                        // program

    String SA = JOptionPane.showInputDialog("What is your savings amount? "); // Window
                                                                                // pops
                                                                                // up
                                                                                // asking
                                                                                // for
                                                                                // your
                                                                                // savings
                                                                                // amount.
                                                                                // As
                                                                                // a
                                                                                // string?

    savingAmount = Double.parseDouble(SA); // Returns a double given to
                                            // value in string above

    String AIR = JOptionPane.showInputDialog("What is the annual interest rate? "); // Window
                                                                                    // pops
                                                                                    // up
                                                                                    // asking
                                                                                    // for
                                                                                    // annual
                                                                                    // interest
                                                                                    // rate.
                                                                                    // String
                                                                                    // as
                                                                                    // above.

    annualInterestRate = Double.parseDouble(AIR); // Returns the same as
                                                    // savings amount but
                                                    // for annual interest
                                                    // rate

    {
        JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is "
                + compoundValueMethod(savingAmount, annualInterestRate, 6));
    }

}
}

2 个答案:

答案 0 :(得分:1)

函数调用应该有括号

试试这个:

 JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is " + compoundValueMethod(savingAmount,annualInterestRate,6));

你的函数应该返回计算值,但在你的情况下你返回'void',所以它应该是这样的

public static int compoundValueMethod(double savingAmount, double annualInterestRate, int sixthMonth) {
int res = 0;
DecimalFormat formatter = new DecimalFormat(".00");
sixthMonth = 6;
double monthlyInterestRate = annualInterestRate / 12;

if (savingAmount < 0)
    if (annualInterestRate < 0) {
        JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
        System.exit(0);
    }

if (savingAmount < 0) {
    JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
}

else if (annualInterestRate < 0) {
    JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
}

else {
    for (int i = 1; i <= 6; i++) {
        res = (int) ((savingAmount + res ) * (1 + monthlyInterestRate));
    }

}
return res;
}

答案 1 :(得分:0)

根据您编写的当前代码,该函数未正确调用,应如下所示:

JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is " + compoundValueMethod(savingAmount,annualInterestRate,6));

如果你总是希望将6的值传递给你的函数,那么你最好不要将它作为函数的参数,你应该更好地宣布一个像 static final 这样的类变量:

private final static int SIXTH_MONTH = 6;

如果你想读取月数,那么你的函数应该具有该参数,你应该读取该参数,就像你正在阅读的其他2个参数一样并传递它。您应该删除为方法参数指定值6的行。

sixthMonth = 6;

而是使用从main()函数传递给函数的值