首页 > 解决方案 > 我的代码上的计算不起作用,为什么?我们必须找到一个房间的平方英尺,然后找出它需要多少油漆 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();
  }

}

标签: javacalculation

解决方案


推荐阅读