【问题标题】:How do I call a method into my main method so it can execute the program?如何将方法调用到我的 main 方法中以便它可以执行程序?
【发布时间】:2016-02-24 02:57:56
【问题描述】:

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

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

我在将新方法调用到主要方法中以执行计算时遇到问题。主要方法是询问用户他们的年利率和储蓄金额。它调用新方法,如果都是正数,它就会执行程序。如果有任何负数,则会出现错误。

我以为我在最后一行调用了该方法,但显然我错了。我试过谷歌搜索,但无法理解这个调用方面。任何信息,将不胜感激。我是新手,所以我还在做这个编程的事情!不知道为什么最后一个大括号不在代码中。代码如下:

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));
    }

}
}

【问题讨论】:

    标签: java methods static-methods


    【解决方案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;
    }
    

    【讨论】:

    • 它表示 + 运算符对于 String void 的参数类型未定义。这未定义怎么办?
    • 现在说第六个月无法解析为变量。是不是在参数范围内建立的?
    • 也就是说,你的范围有问题
    • 我已经更新了我的程序。无论我输入什么数字,我都会返回六个。抱歉所有问题,但为什么它只输出?
    【解决方案2】:

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

    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() 函数传入函数的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 2015-01-08
      • 2013-05-16
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多