java - 我的一个方法使用在另一个方法中返回的变量。我无法使用第二种方法来打印或计算

标签 java methods

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

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

我尝试将 costOfPaint 方法中的语句写入到 main 方法中,并且它有效。但这并没有帮助,因为我需要方法来做到这一点。我知道我的问题与 PaintNeeded 变量有关,我只是不确定如何解决这个问题。

公共(public)类主要{

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));

}

}

对于我的绘制成本,我一直得到 0。

最佳答案

您无需更改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));

关于java - 我的一个方法使用在另一个方法中返回的变量。我无法使用第二种方法来打印或计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993683/

相关文章:

java - 更改可执行 Jar 文件的图标

java - XMLStreamReader 跳过没有属性的元素

java - 必须输入两次才能扫描仪读取

带有正文的 Java "abstract like"方法

java - 我在使用 CompareTo 时遇到问题

java - 如何在单行中绘制具有不同颜色的折线图

java - 如何使用 Undertow 启用跨域源

java - NetworkDispatcher.run : Unhandled exception java. lang.SecurityException : Permission denied (missing INTERNET permission?)

Java:将方法调用存储在数组中并稍后执行?

c++ - endl 是一种方法吗?如果是,它可以接受参数吗?