如何从 main 调用方法,即使我没有在 main 中定义变量。

How to call a method from main, even though I haven't defined variables in main.

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

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

 }

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

flatFee() { // code here }

然后你将a声明为局部变量:

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

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

 picnicCost(flatFee(), MealP(), iceCreamCost());