2016-10-24 43 views
-1

这些是我给我的程序编写的指令: 编写一个程序,计算用于房间的油漆桶数量和最佳购买数量为 。 您需要询问房间的高度以及房间的长度和宽度。房间是长方形的。你必须 油漆墙壁和天花板,但不是地板。没有窗户或天窗。您可以购买以下 大小的油漆桶。 •5升桶每个售价15美元,占地1500平方英尺。 •1升桶的成本为4美元,占地300平方英尺。For循环找出涂漆房间需要多少油漆

我已经写了大部分代码,我只需要帮助计算出使用for循环购买了多少个桶。 这里是我的程序:

public class BrandonLatimerS5L1TryItSolveIt7 { 
public static void main(String[] args){ 
    //declares variables 
    double length; 
    double width; 
    double height; 
    double ceilingArea; 
    double wallsArea; 

    //initializes bucket variables 
    int fiveLiterBucket = 1500; 
    int oneLiterBucket = 300; 

    //Prompts user and gets input for length 
    Scanner input = new Scanner(System.in); 
    System.out.println("Please enter the length of the room in feet: "); 
    length = input.nextDouble(); 

    //prompts user and gets input for width 
    System.out.println("Please enter the width of the room in feet: "); 
    width = input.nextDouble(); 

    //Prompts for input and gets input for height 
    System.out.println("And lastly, please enter the height of the room in feet: "); 
    height = input.nextDouble(); 

    //figures out the total area that needs to be painted 
    ceilingArea = length * width; 
    wallsArea = (2 * (width * height) + (2 * (length * height))); 
    double totalArea = ceilingArea + wallsArea; 

    //For loop to figure out how much paint will be needed. 
    for(int numOfBuckets = 0; totalArea > 1; numOfBuckets++){ 
     totalArea = totalArea - (totalArea/1500); 
     System.out.println("You will need " + numOfBuckets + " buckets."); 
     continue; 

    /* 
    * This program taught me to use the for loop. I just can't seem to figure out how to find the amount of paint I need to buy. 
    */ 
    } 
} 

任何帮助,不胜感激!

+1

这完全是数学。一个for循环不应该是必需的。 – Carcigenicate

回答

0

A for循环不适合这份工作。当程序事先知道时,请使用for循环循环应执行多少次。如果你不知道,或者无法计算循环体应执行多少次,请使用while循环,或者在这种情况下,只使用算术。

只要做到这些步骤的顺序,而无需使用一个循环:

  • 鸿沟总面积1500找出漆的许多大水桶如何购买。
  • 将该数字乘以1500以找出将要覆盖的区域。
  • 从总面积中减去该面积以找出剩余的空白墙面空间。
  • 将剩余的墙面空间除以300以找出需要购买的小桶数量。
  • 使用与上述相同的方法来决定是否需要为剩余的空白墙空间额外添加一个小桶。