-5

我还没有在 main 上初始化任何东西。我想要的只是调用一个外部方法。但是,当调用 picnicCost() 时,我不知道在括号内放什么,因为我没有在 main.xml 中使用任何变量。

import java.util.*;
public class picnic
{
static Scanner scan=new Scanner(System.in);
public static void main(String args[])
{
picnicCost(0,0,0);  
}

public static double flatFee(double a)
{
    System.out.println("Enter the number of people attending: ");
    a=scan.nextDouble();
    return a*5.00;
}

public static double MealP(double b)
{
    System.out.println("Enter the number of poeple purchasing a meal: ");
    b=scan.nextDouble();
    return b*2.75;  
}

public static double iceCreamCost(double c)
{
    System.out.println("Enter the number of poeple purchasing ice cream: ");
    c=scan.nextDouble();
    return c*.75;   
}


 public static double picnicCost(double a, double b, double c)
 {
     return flatFee(a) + MealP(b) + iceCreamCost(c);     
 }

 }
4

1 回答 1

2

如果你需要先验信息来做你想做的事情,你应该只传递一些东西作为参数,所以flatfee和 co 的参数。应该为空:

flatFee() { // code here }

然后声明a为局部变量:

flatFee() {
    double a;
    // do stuff
    return a * 5.0;
}

之后,您可以直接将方法的结果作为参数传递,而无需使用如下变量:

 picnicCost(flatFee(), MealP(), iceCreamCost());
于 2016-07-28T16:55:56.180 回答