【问题标题】:Incorrect output from Delivery Class for Shipping Charge Program运费计划的 Delivery Class 输出不正确
【发布时间】: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;
    }        
}

【问题讨论】:

    标签: java class output


    【解决方案1】:

    这就是为什么你每次都得到 0.0。

    您声明了public float fee;,其默认值为 0.0。

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 2016-10-04
      • 2014-12-19
      • 2020-01-16
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      相关资源
      最近更新 更多