简体   繁体   English

不带参数并返回双精度的方法

[英]method that takes no arguments and returns double

I need to write a public method costMultiplier() that takes no arguments and returns a double.我需要编写一个不带参数并返回双精度值的公共方法 costMultiplier()。 The method should return 1.2 for postcodes beginning “WC1A” or “EC1A”, and 1.0 otherwise.对于以“WC1A”或“EC1A”开头的邮政编码,该方法应返回 1.2,否则返回 1.0。

This is what I have so far but I have added an argument.这是我到目前为止所拥有的,但我添加了一个论点。 I am not sure how to answer the question without the argument and how would I test ir accurately.我不知道如何在没有参数的情况下回答问题,以及如何准确地测试 ir。 Many thanks非常感谢

public double costMultiplier(final String postCode) {
    double multiplier = 1.0d; `

    String pc = postCode.trim().toUpperCase(); 

    if (pc.startsWith("WC1A") || pc.startsWith("EC1A")) {
        multiplier = 1.2; 
    }

    return multiplier;
}

or I did this but need to include a String???或者我做了这个但需要包含一个字符串???

`public static double costMultiplier(){

if(postCode.contains("WC")

return 1.2; } else if(postCode.contains("EC") { return 1.0;} } else if(postCode.contains("EC") { return 1.0;}

Perhaps you can create a PostCode class like:也许您可以创建一个PostCode类,例如:

public class PostCode {

  private final String code;

  public PostCode(String code) {
    this.code = code.trim().toUpperCase();
  }

  public double costMultiplier() {
    if (code.startsWith("WC1A") || code.startsWith("EC1A")) {
        return 1.2; 
    }
    return 1.0
  }

}

then you can do:那么你可以这样做:

new PostCode("WC1A").costMultiplier()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM