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方法,我不知道如何正确返回它。我一直收到错误
PaintJobEstimator.java:99: error: incompatible types: void cannot be converted to double
return System.out.print("To paint" + sqft + "square feet, with");
^
我在做错了什么。或者我完全忽略了这一点。我是新手,真的需要帮助,我不想得到答案但是如果有人可以指出我的方向很好
答案 0 :(得分: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();
答案 1 :(得分:0)
System.out.print(...)
打印给定对象输出。
return
用于将值传递给函数的调用者,在本例中为main
方法。由于您的generateReport
方法属于double
类型,因此您必须返回一些double
值。如果您不想返回任何内容,那么您的方法应为void
。
System.out.print(...)
是一个void
函数。