【发布时间】:2019-03-31 22:23:45
【问题描述】:
我在最近的作业中遇到了这段代码的困难。该任务向用户显示汽油价格,询问他们想要什么类型以及多少加仑。该程序将总价格作为双倍返回。我在方法 calculatePrice 中创建了一个可以返回答案的开关。我无法收集该信息并以某种方式将其打印到方法 displayTotal。此外, displayTotal 必须是双精度数。任何帮助都将不胜感激。
public static double calculatePrice(int type, double gallons){
switch (type){
case 1:
System.out.printf("You owe: %.2f" , gallons * 2.19);
break;
case 2:
System.out.printf("You owe: %.2f", gallons * 2.49);
break;
case 3:
System.out.printf("You owe: %.2f", gallons * 2.71);
break;
case 4:
System.out.printf("You owe: %.2f", gallons * 2.99);
}
return type;
}
public static void displayTotal(double type){
System.out.println(type);
}
}
【问题讨论】:
-
有什么问题?
-
您需要将加仑和价格/加仑相乘的结果保存在一个变量中并返回。