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

时间:2016-07-28 16:48:05

标签: java main-method

我还没有在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);     
 }

 }

1 个答案:

答案 0 :(得分:2)

如果您需要先前的信息来做您想做的事情,那么您应该只将某些内容作为参数传递,因此flatfee和co的参数。应该是空的:

flatFee() { // code here }

然后将a声明为本地变量:

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

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

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