java - 我的代码上的计算不起作用,为什么?我们必须找到一个房间的平方英尺,然后找出它需要多少油漆 1gallon=350sqft
问题描述
我们必须找到一个房间的平方英尺并确定它需要多少油漆。油漆加仑覆盖面积 350 平方英尺。出于某种原因,我的代码在计算中不起作用。我知道问题出在“if/else”语句中,我只是不知道是什么。我做了一个 if-else 声明,说如果平方英尺低于 350,那么只需要制作所需的加仑数, 1. 只是因为我们不应该将其保留为小数,因为如果你去商店,它应该是,你不能按十进制大小。谢谢
//import java scanner
import java.util.Scanner;
//declare variables
class Main {
final int COVERAGE =350; //paint covers 350sq. feet
int length, width, height;
double totalSqFt, paintNeeded;
//declare integers length, width, height
//declare doubles totalSqFt, paintNeeded
Scanner scan = new Scanner(System.in);
//this will run all the main methods
public void runProgram()
{
input();
calculations();
output();
}
//this will get the user input for the length, height, and width of the room
public void input()
{
System.out.println("Length of room:");
length = scan.nextInt();
scan.nextLine();
System.out.println("Height of room:");
height = scan.nextInt();
scan.nextLine();
System.out.println("Width of room:");
width = scan.nextInt();
scan.nextLine();
//Prompt for and read in the length of the room
//Prompt for and read in the height of the room
//Prompt for and read in the width of the room
}
//this calculates the user data for the amount of paint needed
public void calculations()
{
totalSqFt = height*width*2+height*length*2;
if (totalSqFt < 350){
paintNeeded = 1;
}else{
paintNeeded = (int)(totalSqFt / COVERAGE);
}
//Compute the total square feet of the walls to be painted
//Computer the amount of paint needed
}
//this prints out the calculations above
public void output()
{
System.out.println("Length: " + length + " Width: " + width + " Height: " + height);
System.out.println("Total Square Feet: " + totalSqFt + " \nGallons of Paint Needed: " + paintNeeded);
//Print the length, width, and height of the room and the gallons of paint needed.
}
public static void main(String[] args)
{
Main prog = new Main();
prog.runProgram();
}
}
解决方案
推荐阅读
- reactjs - 成功部署 Firebase 后的空白页面
- php - 如何在 php 中找到嵌套 foreach 循环的迭代
- react-native - React Navigation:使用自定义导航器时,“无法将没有 YogaNode 的子代添加到没有测量功能的父代”
- python - 如何使用python win32在outlook中更改发送邮件的地址
- ios - NSLocationWhenInUseUsageDescription 错误继续显示
- rest - 如何将空行添加到网格并填充列,然后在 EXTJS 中提交
- reactjs - 使用 API 调用 unstated.js (React Context API) 初始化容器
- sql - 在 bigquery 中将 DATETIME 格式转换为自定义格式
- pyspark - Pyspark:K表示距离或偏差的结果?
- javascript - React & Redux:“表单提交因表单未连接而取消”