什么是格式化程序的替代品?

来源:爱站网时间:2021-11-22编辑:网友分享
什么是格式化程序的替代品?这是爱站技术小编最近正在研究的学习课题,小编觉得能用一篇文章解决的事情就不用说很多什么了,直接上一篇干货给大家看看吧。

问题描述


我有这堂课:

class SportsCar extends Car{ 
 private String myColor; // color of sports car
 private double myEngine; // engine size in liters
 private String mySuspension; // firm, soft, touring, etc.
 private String myTires; // regular, wide, low profile, etc.

 // constructor
 public SportsCar(String brand, String type, int price, double mileage,
 double gasPrice, int miles, int passengers, int numDoors, String color,
 double engine, String susp, String tires){
 // uses Car's constructor
 super(brand, type, price, mileage, gasPrice, miles, passengers, numDoors);

 // initializes what's unique to SportsCar
 myColor = color; myEngine = engine; mySuspension = susp; myTires = tires;
 }

 public String getColor(){
 return myColor;
 }

 public double getEngine(){
 return myEngine;
 }

 public String getSuspension(){
 return mySuspension;
 }

 public String getTires(){
 return myTires;
 }
 // overloads toString() method() using super
 public String toString(){
 return super.toString() + "\nYour cool " + myColor + " sports car has a "
 + formatter(“%.2f”, myEngine) + " liter engine, " + mySuspension
 + " suspension and " + myTires + " tires.";
 }
} 

当前,它应该正确打印出我们的图像。一个是我不能使用格式化程序打印出myEngine。除了使用格式化程序,还有其他方法可以以升为单位正确打印myEngine吗?我目前不知道其他任何方式。

思路:


您可能想使用包装器类。

like

private "Double" myEngine;
System.out.println(myEngine);

https://www.geeksforgeeks.org/wrapper-classes-java/

尽管如此,格式化程序实际上是这种情况下的最佳速记。您也可以使用Wrapper来实现,但这将是更多的代码。因此:更好地使用格式化程序。 ;)

https://www.geeksforgeeks.org/wrapper-classes-java/

以上内容就是爱站技术频道小编为大家分享的什么是格式化程序的替代品?看完以上分享之后,大家应该都知道是什么了吧。

上一篇:为什么需要在主方法中抛出NullPointerException?

下一篇:父pom中的Maven子模块作为依赖项的表达

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载