3 回答

TA貢獻(xiàn)1856條經(jīng)驗 獲得超5個贊
如果要在costPaint方法中使用requiredPaint方法,則需要強(qiáng)制向 requiredPaint 方法發(fā)送雙參數(shù)?,F(xiàn)在這完全取決于您將如何實現(xiàn)您的功能。
您可以將 double 類型的參數(shù)添加到您的 costForPaint 方法中,如下所示:
public static double costForPaint(double totalSquareFeet) {
double paintCost = 2;
//shows error where required paint is
double totalPaintCost = requiredPaint(totalSquareFeet) * paintCost;
return totalPaintCost;
}

TA貢獻(xiàn)1943條經(jīng)驗 獲得超7個贊
你的代碼應(yīng)該像-
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() 是一個參數(shù)化方法,并且您沒有在其中提供任何參數(shù)。

TA貢獻(xiàn)1850條經(jīng)驗 獲得超11個贊
1> 在 Java 中,如果您已經(jīng)向方法聲明了一個參數(shù),則必須傳遞該參數(shù)(空值或某個值)。這是一個可以根據(jù)您的要求完成的流程。
2> 你也不能在類塊中調(diào)用方法。所以我刪除了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;
}
}
添加回答
舉報