互联网服务提供商为其客户提供三种不同的订阅优惠:
• Offer A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour
• Offer B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour
• Offer C: For $19.95 per month unlimited access is provided.
编写一个程序 Internet.java,获取客户已购买的商品(A、B 或 C)和使用的小时数。然后它应该显示总费用。
> java Internet A 20
The total charges: $29.95
我已经编写了我的代码,但由于某种原因它一直显示错误的输出:
public class Internet {
public static void main(String [] args){
String offer = args[0];
int hours = Integer.parseInt(args[1]);
String error = "error";
charge(offer,hours);
}
public static double charge(String offer, int hours){
char c = offer.charAt(0);
double PackA = 9.95;
double PackB = 13.95;
double PackC = 19.95;
if (c == 'A'){
PackA = (hours - 10)*2.00;
System.out.println("the total charges: " + PackA);
return PackA;
}
else if(c == 'B'){
PackB = (hours - 20)*1.00;
System.out.println("the total charges: " + PackB);
return PackB;
}
else if (c == 'C') {
System.out.println("the total charges: " + PackC);
return PackC;
}
return error;
}
}