首页 > 解决方案 > 我的一种方法使用在另一种方法中返回的变量。我无法获得第二种打印或计算方法

问题描述

一家油漆公司已确定,每 115 平方英尺的墙壁空间,将需要一加仑油漆和八小时的劳动。公司每小时收取 18.00 美元的人工费。编写一个程序,允许用户输入要粉刷的房间数量和每加仑油漆的价格。它还应该要求每个房间的墙壁空间平方英尺。该程序应具有返回以下数据的方法: • 所需的油漆加仑数 • 所需的劳动小时数 • 油漆的成本 • 人工费用 • 油漆工作的总成本 然后它应该显示数据屏幕。

我还没有解决这个问题的劳动部分,但由于 paintNeeded 变量,我无法打印 costOfPaint()。

我已经尝试将 costOfPaint 方法中的语句写入 main 方法并且它有效。但这无济于事,因为我需要这样做的方法。我知道我的问题与paintNeeded 变量有关,我只是不确定如何解决这个问题。

公共类主{

public static double paintRequired(double totalSquareFeet){

    double paintNeeded = totalSquareFeet / 115;
    return paintNeeded;

                                                          }

public static double costOfPaint(double paintNeeded, double costOfPaint){
    double paintCost = paintNeeded * costOfPaint;
    return paintCost;
                                                    }


public static void main(String[] args){
    double costOfPaint = 0;
    int totalSquareFeet = 0;
    double paintNeeded = 0;

    Scanner getUserInput = new Scanner(System.in);

    System.out.println("what is the cost of the paint per gallon");
        costOfPaint = getUserInput.nextDouble();
    System.out.println("How many rooms do you need painted");
        int rooms = getUserInput.nextInt();
            for(int i = 1; i <= rooms; i++){
                System.out.println("how many square feet are in room:" + i);
                int roomSquareFeet = getUserInput.nextInt();
                totalSquareFeet = roomSquareFeet + totalSquareFeet;
                                           }

    System.out.println("the amount of paint needed:" + paintRequired(totalSquareFeet) + "gallons");
    System.out.println("the cost of the paint will be: " + costOfPaint(paintNeeded, costOfPaint));

}

}

对于我的 costOfPaint,我一直得到 0。

标签: javamethods

解决方案


你不改变paintNeeded。它总是0

paintNeeded = paintRequired(totalSquareFeet);

System.out.println("the amount of paint needed: " + paintNeeded + " gallons");
System.out.println("the cost of the paint will be: " + costOfPaint(paintNeeded, costOfPaint));

推荐阅读