运费计划的交货类输出不正确

时间:2015-02-13 23:53:58

标签: java class output

所以我编写了整个程序,但输出费用输出不正确。我删除了我写下来的只是一个计算,看看我做错了什么,但仍然无法弄明白。方向如下:

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;
    }        
}

1 个答案:

答案 0 :(得分:1)

这就是你每次都获得0.0的原因。

您宣布public float fee;的默认值为0.0。

getTotal()方法中,您返回fee - 请注意,在此处,您将返回上面声明的公共浮动费用,并且您从未将其从默认值更改。

问题在于你的getTotal()。这里的主要问题是您的else声明之后没有else if。那么当你第一个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;
    }
}