【发布时间】:2015-02-13 23:53:58
【问题描述】:
所以我编写了整个程序,但我的运费输出不正确。我删除了我写下来的内容,只进行了一次计算,以查看我做错了什么,但仍然无法弄清楚。方向如下:
您受雇于 Expressimo Delivery Service,负责开发一个计算运费的应用程序。公司允许两种类型的包装信和盒子以及三种类型的服务 - 次日优先,次日标准和2天/下表显示了计算费用的公式。
套餐类型 次日优先 次日标准 2天
信函 $12 最多 8 盎司 $10.50 最多 8 盎司 不可用
盒装第一磅 15.75 美元。在第一磅的基础上每增加一磅,增加 1.25 美元。第一磅 13.75 美元。在第一磅的基础上每增加一磅就增加 1.00 美元。第一磅 7.00 美元。在第一磅的基础上每增加一磅,就增加 0.50 美元。
到目前为止,我只是想弄清楚一个字母的简单计算。我编写了代码来计算第一行指令!但是每笔运费我都会得到 0.0。这个有点混乱,所以我希望我解释得对。我不能对这个程序使用数组或循环。这是我的驱动程序类:
public class ExpressimoDelivery {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String pkg, service;
int ounces;
Scanner input = new Scanner(System.in);
System.out.println("Enter the package type (letter or box): ");
pkg = input.next();
System.out.println("Enter the type of delivery service (P - for Priority, S - for Standard, TWO - for 2 Day): ");
service = input.next();
System.out.println("Please enter the weight of the package in ounces: ");
ounces = input.nextInt();
Delivery customer = new Delivery(pkg, service, ounces);
customer.setPackage(pkg);
customer.setService(service);
customer.setWeight(ounces);
System.out.println("You have chosen to mail a " + pkg + "\nand the cost"
+ " to mail a " + pkg + " weighing " + ounces + " ounces with " + service + " service is: "
+ customer.getTotal());
}
}
这是我的交付课程:
public class Delivery {
public static final float LETTER_PRIORITY = 12.00f;
public static final float LETTER_STANDARD = 10.50f;
public static final float BOX_PRIORITY = 15.75f;
public static final float BOX_STANDARD = 13.75f;
public static final float BOX_TWO_DAY = 7.00f;
public static final float ADD_PRIORITY = 1.25f;
public static final float ADD_STANDARD = 1.00f;
public static final float ADD_TWODAY = 0.50f;
public String pkg, service;
public int weight = 0;
Scanner scanner = new Scanner(System.in);
public float fee;
public Delivery(String pkg, String service, int weight){
this.pkg = pkg;
this.service = service;
this.weight = weight;
}
public void setPackage(String pkg){
this.pkg = pkg;
}
public void setService(String service){
this.service = service;
}
public void setWeight(int weight){
this.weight = weight;
}
public String getPackage(){
if (! (pkg.equals("letter") || pkg.equals("box")))
{
System.out.println("ERROR: Unknown package type");
pkg = "";
}
return pkg;
}
public String getService(){
if (! (service.equals("P") || service.equals("S") || service.equals("TWO")));{
System.out.println("ERROR: Unknown delivery type");
service = "";
}
return service;
}
public float getWeight(){
return weight;
}
public float getTotal(){
if(pkg.equals("letter") && service.equals("P") && weight <= 8){
fee = LETTER_PRIORITY;
} else if(weight > 8) {
System.out.println("Package too large for a letter. Please use box instead.");
}
return fee;
}
}
【问题讨论】: