调用带有参数的方法到另一个方法

时间:2018-08-27 04:28:31

标签: java

im试图找出如何或是否有可能调用其中带有参数的方法。我想在costForPaint方法中使用requiredPaint方法。 (代码已简化):

public class job {

public static void main(String[] args) {

    int rooms = 1;
    double squareFeet = 0;
    double totalSquareFeet = 115;

    totalSquareFeet = squareFeet + totalSquareFeet;

    }

requiredPaint(totalSquareFeet);

//i want to use the totalSquareFeet in this method, this is why it is called
public static double requiredPaint(double totalSquareFeet) {

    double totalPaint = totalSquareFeet / 115;

    return totalPaint;

}

public static double costForPaint() {

    double paintCost = 2;

    //shows error where required paint is
    double totalPaintCost = requiredPaint() * paintCost;

    return totalPaintCost;
}

5 个答案:

答案 0 :(得分:1)

如果要在 costPaint 方法中使用 requiredPaint 方法,则需要强制将double参数发送到requiredPaint方法。 现在,这完全取决于您将如何实现功能。

您可以将double类型的参数添加到costForPaint方法中,如下所示:

public static double costForPaint(double totalSquareFeet) {

    double paintCost = 2;

    //shows error where required paint is
    double totalPaintCost = requiredPaint(totalSquareFeet) * paintCost;

    return totalPaintCost;
}

答案 1 :(得分:0)

调用需要参数的方法是完全可能的,而且很常见。您无法调用方法 requirePaint(),因为您试图从无效的地方调用它。将方法放入 main()方法中,它应该可以正常工作。看下面:

public static void main(String[] args) {

    int rooms = 1;
    double squareFeet = 0;
    double totalSquareFeet = 115;

    totalSquareFeet = squareFeet + totalSquareFeet;
    requiredPaint(totalSquareFeet); //here
}

答案 2 :(得分:0)

1>在Java中,如果已为method声明了参数,则必须传递该参数(null或某个值)。这是可以根据您的要求完成的流程。

2>同样,您不能在类块中调用方法。所以我删除了requiredPaint(totalSquareFeet);

public class job {
    public static void main(String[] args) {
        int rooms = 1;
        double squareFeet = 0;
        double totalSquareFeet = 115;
        totalSquareFeet = squareFeet + totalSquareFeet;
        costForPaint(totalSquareFeet);
    }

    //i want to use the totalSquareFeet in this method, this is why it is called
    public static double requiredPaint(double totalSquareFeet) {
        double totalPaint = totalSquareFeet / 115;
        return totalPaint;
    }

    public static double costForPaint(double totalSquareFeet) {

        double paintCost = 2;

        //shows error where required paint is
        double totalPaintCost = requiredPaint(totalSquareFeet) * paintCost;

        return totalPaintCost;
    }
}

答案 3 :(得分:0)

您的代码应为Like-

public static double costForPaint() {

     double paintCost = 2;
     double totalPaint = 15;//any number you want to initialize in totalPaint 

     double totalPaintCost = requiredPaint(totalPaint) * paintCost;

     return totalPaintCost;
}

您遇到错误,因为requiredPaint()是参数化方法,并且没有在其中提供任何参数。

答案 4 :(得分:0)

下面是代码中的问题。

1)您没有遵循Java命名约定(用CamelCase(Job)声明类名)

2)您已经使用双参数定义了方法requiredPaint,但是您没有任何方法调用它。

class job {
public static void main(String[] args) {
    int rooms = 1;
    double squareFeet = 0;
    double totalSquareFeet = 115;
    totalSquareFeet = squareFeet + totalSquareFeet;
    requiredPaint(totalSquareFeet);
}

//i want to use the totalSquareFeet in this method, this is why it is called
public static double requiredPaint(double totalSquareFeet) {
    double totalPaint = totalSquareFeet / 115;
    return totalPaint;
}

public static double costForPaint() {
    double paintCost = 2;
    //Passing paintCost parameter to method
    double totalPaintCost = requiredPaint(paintCost) * paintCost;
    return totalPaintCost;
 }
}