[ad_1]
运送包裹的费用为 75 美分的固定费用加上每磅 25 美分。
1. 声明一个名为 CENTS_PER_POUND 的常量并使用 25 进行初始化。
2. 从用户输入中获取运输重量,并将重量存储到shipWeightPounds 中。
3. 使用 FLAT_FEE_CENTS 和 CENTS_PER_POUND 常量,将shipCostCents 指定为运输称重shipWeightPounds 的包裹的成本。
我尝试过的:
爪哇
import java.util.Scanner; public class ShippingCalculator { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int shipWeightPounds; int shipCostCents = 0; final int FLAT_FEE_CENTS = 75; final double CENTS_PER_POUND = 25; shipWeightPounds = scnr.nextDouble(); shipCostCents = FLAT_FEE_CENTS + (CENTS_PER_POUND * shipWeightPounds); System.out.println("Weight(lb): " + shipWeightPounds); System.out.println("Flat fee(cents): " + FLAT_FEE_CENTS); System.out.println("Cents per pound: " + CENTS_PER_POUND); System.out.println("Shipping cost(cents): " + shipCostCents); } }
解决方案1
代替
引用:最终双倍 CENTS_PER_POUND = 25;
ShipWeightPounds = scnr.nextDouble();
和
final int CENTS_PER_POUND = 25; shipWeightPounds = scnr.nextInt();
解决方案2
引用:我缺少什么? 我尝试了很多方法来解决这个问题。
不是你的问题,而是:
所有这些
爪哇
int shipCostCents = 0; final int FLAT_FEE_CENTS = 75; final double CENTS_PER_POUND = 25;
是美分。
为什么要混合数据类型?
始终如一 !
[ad_2]
コメント