java - 我的一种方法使用在另一种方法中返回的变量。我无法获得第二种打印或计算方法
问题描述
一家油漆公司已确定,每 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。
解决方案
你不改变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));
推荐阅读
- qt - 捕捉 QML 组件状态/进度变化信号
- java - Spring Integration IMAP:由于 Outlook 365 中的病毒扫描,无法从邮件中读取附件
- c# - 声明数据库上下文的控制器级实例
- c++ - 为什么使用“return”-fpermissive 出现 void 错误问题
- gitlab - 使 SonarQube 外部作业有条件地使管道失败
- java - 在 TreeViewer 中添加子项展开
- html - 引导数据目标/aria-controls 不触发 id 元素
- flutter - 在颤振中使用codemagic增加应用程序版本
- c++ - 在二叉搜索树上没有得到输出,有什么方法可以帮助我找到我在 C++ 中的错误吗?
- python - 我想将函数变量初始化为先前输入的变量