因此,我编写了整个程序,但交付的输出不正确。我删除了我写的只是一个计算,以查看我做错了什么,但仍然无法弄清楚。指示如下:
Expressimo Delivery Service雇用您来开发一个计算运费的应用程序。该公司提供两种包装信件和盒装服务,以及三种服务类型-次日优先,次日标准和2天/下表显示了计算费用的公式。
套餐类型第二天优先第二天标准2天
Letter 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;
}
}
最佳答案
这就是为什么每次都能获得0.0的原因。
您声明了public float fee;
,其默认值为0.0。
在您的getTotal()
方法中,您返回fee
-注意,在这里,您将返回上面声明的公众持股量费用,并且从未更改默认值。
问题出在您的getTotal()中。这里的主要问题是else
语句后没有else if
。那么,如果您在第一个if语句中失败,而在else if中失败了,会发生什么呢?没有任何反应,费用没有变化,因此您获得0.0的回报。
解:
public float getTotal(){
if(pkg.equals("letter")) {
if(weight > 8) {
// quit early since it's too big
// do your system.out.print here
// return 8.8 since the size is too big; this is kind of
// like an error code
return 8.8;
}
if(service.equals("P")) {
// service is priority
fee = LETTER_PRIORITY;
} else {
// here, the service is not priority, so put the non priority letter calculations here
}
} else if (pkg.equals("box")) {
// do your box fee checks and calculations here
} else {
// the pkg is not a box or a letter - something is wrong, give fee
// a default value, for example 9.99 or whatever you want.
// make it something so that you KNOW that your pkg went into this
// loop and something went wrong
fee = 9.99;
}
}
关于java - 运费计划的交货类别输出不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28510737/