2016-01-27 93 views
-2
import java.util.*; 


// This program will estimate the cost to paint a room in your house 

public class PaintJobEstimator { 

    // square feet per one gallon of paint. 
    public static final double AREA_PER_GALLON = 112.0; 

    // hours of labor needed to paint AREA_PER_GALLON square feet. 
    public static final double HOURS_PER_UNIT_AREA = 8.0; 

    // charge to customer for one hour of labor. 
    public static final double LABOR_COST_PER_HOUR = 35.0; 

    // main declares a Scanner that is passed to 
    // the input methods. main also controls the 
    // order of calculations. 
    public static void main(String[] args) { 
     Scanner keyboard = new Scanner(System.in); 

     // How many square feet do we need to paint? 
     double sqft = getInput(keyboard, 
         "Enter the number of square feet: "); 

     // How much does a gallon of paint cost? 
     double gallonCost = getInput(keyboard, 
         "Enter the price of a gallon of paint: ");   
     //////////////////////////////////////// 
     // Calculate the cost of this paint job. 
     //////////////////////////////////////// 

     // First, how many gallons of paint do we need? 
     int numGallons = calculateGallons(sqft); 

     // How long will the job take? 
     double hoursLabor = calculateHours(sqft); 

     // How much will the paint cost? 
     double paintCost = calculatePaintCost(numGallons, gallonCost); 

     // How much will the labor cost? 
     double laborCost = calculateLaborCost(hoursLabor); 

     // What's the total bill? 
     double totalCost = calculateTotalCost(paintCost, laborCost); 

     // Print the results. 
     generateReport(sqft, gallonCost, numGallons, hoursLabor, 
         paintCost, laborCost, totalCost); 

    } 

    public static double getInput(Scanner input, String prompt) { 
     System.out.print(prompt); 
     while (!input.hasNextDouble()) { 
      input.nextLine(); // get rid of bad input. 
      System.out.print(prompt); 
     } 
     double inValue = input.nextDouble(); 
     input.nextLine(); // clear the input line. 
     return inValue; 
    } 


    // Your methods go here: 

    // calculateGallons 
    public static int calculateGallons(double sqft) { 
     // TO DO return correct value 
     return (int)Math.ceil(sqft/AREA_PER_GALLON); 
    } 

    // calculateHours 
    public static double calculateHours(double sqft) { 
     // TO DO return correct value 
     return sqft/14; 
    } 

    // TO DO: calculatePaintCost 
    public static double calculatePaintCost (int numGallons, double gallonCost){ 
     return numGallons * gallonCost; 
    } 

    // TO DO: calculateLaborCost (Hours * Labor/hr) 
    public static double calculateLaborCost(double hoursLabor){ 
     return hoursLabor * LABOR_COST_PER_HOUR; 
    }  
    // TO DO: calculateTotalCost 
    public static double calculateTotalCost(double paintCost, double laborCost){ 
     return paintCost + laborCost; 
    } 

    // To Do: generateReport 
    public static double generateReport(double sqft, double gallonCost, int numGallons, double hoursLabor, double paintCost, double laborCost, double totalCost) { 
     return System.out.print("To paint" + sqft + "square feet, with"); 
       System.out.print("paint that costs" + gallonCost + "per gallon,"); 
       System.out.print("you will need" + numGallons + "gallons of paint"); 
       System.out.print("and" + hoursLabor + "hours of labor."); 
       System.out.print("The cost of the paint is: " + paintCost); 
       System.out.print("The cost of the labor is: "+ laborCost); 
       System.out.print("The total cost of the job is: " + totalCost); 
     System.out.println(); 
    } 




} 

我有使用generateReport方法,我不知道如何正確返回它。我不斷收到錯誤錯誤:不兼容的類型:void無法轉換爲雙倍

PaintJobEstimator.java:99: error: incompatible types: void cannot be    converted to double  
    return System.out.print("To paint" + sqft + "square feet, with"); 
          ^

我做錯了什麼。或者我完全錯過了這一點。我是新來的,真的需要幫助,我不想得到答案,但如果有人可以指向我的方向,那將是很好的

回答

2

System.out.print()返回沒有值;它有一個返回類型void

爲了改寫這個沒有任何錯誤,將其更改爲這樣的事:給對象輸出

public static void generateReport(double sqft, double gallonCost, int numGallons, double hoursLabor, double paintCost, double laborCost, double totalCost) { 
      System.out.print("To paint" + sqft + "square feet, with"); 
      System.out.print("paint that costs" + gallonCost + "per gallon,"); 
      System.out.print("you will need" + numGallons + "gallons of paint"); 
      System.out.print("and" + hoursLabor + "hours of labor."); 
      System.out.print("The cost of the paint is: " + paintCost); 
      System.out.print("The cost of the labor is: "+ laborCost); 
      System.out.print("The total cost of the job is: " + totalCost); 
      System.out.println(); 
0

System.out.print(...)打印。 return用於將值傳遞給函數的調用方,在這種情況下爲main方法。由於您的generateReport方法的類型爲double,因此您必須返回一些double值。如果你不想返回任何東西,那麼你的方法應該是void

System.out.print(...)例如,是void函數。

相關問題