【问题标题】:One of my methods uses a variable that is returned in another method. I cannot get the second method to print or calculate我的一种方法使用在另一种方法中返回的变量。我无法获得第二种打印或计算方法
【发布时间】:2019-09-18 13:15:18
【问题描述】:

一家油漆公司已确定,每 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。

【问题讨论】:

    标签: java methods


    【解决方案1】:

    你不会改变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));
    

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多