Calling a method in java and returning beer cost

后端 未结 3 1679
轻奢々
轻奢々 2021-01-29 13:02

What I am supposed to do:

Add some necessary statements to the printOrderCost() method in the main class so that this method computes and p

3条回答
  •  北海茫月
    2021-01-29 13:32

    It is generally a bad idea to add strings up like you did, as Java will create a unique String for each addition, which causes some unnecessary overhead. You can either use a StringBuilder as a general-purpose tool, or, if you know the exact format of how the String should look like, you can use String.format(...).

    Example:

    public toString() {
      return String.format("%-10s %2d %6.2f %6.2f", brand, quantity, itemCost, getCost());
    }
    

提交回复
热议问题