2016-10-24 41 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以找出需要購買的小桶數量。
  • 使用與上述相同的方法來決定是否需要爲剩餘的空白牆空間額外添加一個小桶。