假設一加侖油漆覆寫了大約350平方英尺的墻壁空間,請用戶輸入長度、寬度和高度。這些方法應執行以下操作:
- 計算房間的墻壁面積
- 將計算出的墻壁面積傳遞給另一個方法,該方法計算并回傳所需油漆的加侖數
- 顯示需要的加侖數
- 根據油漆價格每加侖 32 美元計算價格,假設油漆工可以以與整加侖相同的價格購買一加侖油漆的任何一部分。
- 將價格回傳給 main 方法。main() 方法顯示最終價格。示例:粉刷一個 10 英尺高的 15×20 房間的成本是 64 美元。
這是我所做的,我沒能拿到那 64 美元
public static void main(String[] args){
double l,h,w;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the height: ");
h=sc.nextDouble();
System.out.print("Enter the width: ");
w=sc.nextDouble();
System.out.print("Enter the length: ");
l=sc.nextDouble();
disGallons(calArea(h, w, l));
disPrice(price(calGallon(calArea(h, w, l))));
}
public static double calArea(double h,double w, double l){
double area=2*((w*h) (l*w) (l*h));
return area;
}
public static double calGallon(double area){
double gallons= area/350;
return gallons;
}
public static void disGallons(double gallons){
System.out.println("Gallons needed: " gallons);
}
public static double price(double gallon){
final double gallPrice=32;
return (int)(gallPrice*gallon);
}
public static void disPrice(double price){
System.out.println("Total Price is: $" price);
}
uj5u.com熱心網友回復:
這就是我的做法。
package io.duffymo;
/**
* Size the amount of paint needed
* @link <a href="https://stackoverflow.com/questions/72404779/i-dont-understand-where-does-64-comes-from">...</a>
*/
public class PaintSizing {
public static final double PRICE_PER_GALLON_USD = 32.0;
public static final double SQ_FEET_PER_GALLON = 350.0;
public static double area(double h, double w, double l) {
return 2.0*(w l)*h;
}
public static double gallons(double area) {
return area / SQ_FEET_PER_GALLON;
}
public static double price(double gallons) {
return gallons * PRICE_PER_GALLON_USD;
}
}
學習 JUnit 永遠不會太早:
package io.duffymo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class PaintSizingTest {
@Test
public void testSizing() {
// setup
double l = 15.0;
double w = 20.0;
double h = 10.0;
double expectedArea = 700.0;
double expectedGallons = 2.0;
double expectedPrice = 64.0;
// exercise
double actualArea = PaintSizing.area(h, w, l);
double actualGallons = PaintSizing.gallons(actualArea);
double actualPrice = PaintSizing.price(actualGallons);
// assert
Assertions.assertEquals(expectedArea, actualArea, 0.001);
Assertions.assertEquals(expectedGallons, actualGallons, 0.001);
Assertions.assertEquals(expectedPrice, actualPrice, 0.001);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483566.html
上一篇:可選添加到物件的簡單表示法?
