我该如何调用方法?在Java中

时间:2014-12-08 06:52:08

标签: java methods call

public static void main(String args [])
{
    double dimes;
    double quaters;

    Scanner input = new Scanner(System.in);

    System.out.print("Enter number of Dimes:");
    dimes = input.nextDouble();

    System.out.print("Enter Number of Quaters:");
    quaters = input.nextDouble();

    double dollars= dollar_amount(dimes,quaters);
    System.out.println("Dollar Amount total: $" + dollars);

    public static double dollar_amount(dimes,quaters);
    dollars= dollar_amount(dimes,quaters);
    System.out.println("Total dollar amount: $" + dollars);
}

double dollar_amount(double dimes, double quaters);
{
    dollars = (0.10 * dimes)+(0.25 * quaters);
}

return dollars;

}
}
}

我有一个关于如何调用方法的问题。我遵循了我给出的提示,但不知怎的,我还没有开始调用方法。

提示:

  1. 输入角钱
  2. 输入季度
  3. 调用方法dollar_amout(角钱,四分之一)
  4. dollar = dollar_amount(角钱,宿舍)
  5. end main
  6. double dollar_amount(角钱,宿舍)
  7. 美元= 0.10 x角钱+ 0.25 x季度
  8. 返还美元
  9. 结束方法

3 个答案:

答案 0 :(得分:0)

我认为你的方法体是错误的

Double dollar_amount(double dimes, double quaters);
{
  Double dollars = (0.10 * dimes)+(0.25 * quaters);
  return dollars;   
}

你称之为正确的地方。没问题

答案 1 :(得分:0)

您正在尝试使用方法设置变量的值。您必须先正确定义方法dollar_amount(dimes,quaters),然后才能调用它。

将此代码粘贴到当前代码中,并粘贴到主类中。

public static void main(String args []){
   double dimes;
   double quaters;

   Scanner input = new Scanner(System.in);

   System.out.print("Enter number of Dimes:");
   dimes = input.nextDouble();

   System.out.print("Enter Number of Quaters:");
   quaters = input.nextDouble();

   double dollars= dollar_amount(dimes,quaters);
   System.out.println("Dollar Amount total: $" + dollars);

}

public static double dollar_amount(dimes,quaters){
   dollars = (0.10 * dimes)+(0.25 * quaters);
   return dollars;
}

答案 2 :(得分:0)

你必须做一些改变,试试这样。

public static void main(String args[]) {
    double dimes;
    double quaters;

    Scanner input = new Scanner(System.in);

    System.out.print("Enter number of Dimes:");
    dimes = input.nextDouble();

    System.out.print("Enter Number of Quaters:");
    quaters = input.nextDouble();

    double dollars = dollar_amount(dimes, quaters);
    System.out.println("Dollar Amount total: $" + dollars);

    dollars = dollar_amount(dimes, quaters);
    System.out.println("Total dollar amount: $" + dollars);
}

public static double dollar_amount(double dimes, double quaters) {
    double dollars = (0.10 * dimes) + (0.25 * quaters);
    return dollars;
}