Java 如何从main调用方法,即使我没有在main中定义变量。
我还没有在main上初始化任何东西。我只想调用一个外部方法。然而,当调用野餐成本时,我不知道在括号中放什么,因为我在main中没有使用任何变量Java 如何从main调用方法,即使我没有在main中定义变量。,java,main-method,Java,Main Method,我还没有在main上初始化任何东西。我只想调用一个外部方法。然而,当调用野餐成本时,我不知道在括号中放什么,因为我在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 flatFe
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 and co.的参数应该为空:
flatFee() { // code here }
然后将a声明为局部变量:
flatFee() {
double a;
// do stuff
return a * 5.0;
}
之后,您可以直接将方法的结果作为参数传递,而无需使用如下变量:
picnicCost(flatFee(), MealP(), iceCreamCost());
我当时没有使用任何变量。例如,您可以添加到main:double flatFee=flatFee;。在flatFee方法中不需要这个参数。前三个方法看起来根本不需要参数。使用变量,我认为你会得到预期的结果。你的任何方法看起来都不需要任何更改。预期的行为/输出是什么?