gpt4 book ai didi

java - "The value of the field speed is the average speed in knots"添加值

转载 作者:行者123 更新时间:2023-12-01 11:55:54 25 4
gpt4 key购买 nike

好吧,我是编程新手,我想知道如何打印某些字段的值。例如,我有一个速度,单位是节(21 节),如何将“节”部分添加到我的代码中,这样我就不会只打印整数?我希望它有 [number] 节。这是我到目前为止的代码:

public class Ship {
//fields
private String name;
private double speed;

//constructors
public Ship(String n, double s) {
name = n;
speed = s;
}

//methods
public String getName() {
return name;
}
public double getSpeed() {
return speed; //avg speed in knots
}

public double timeTravel(double distance) {
double travel = speed * 1.151;
return distance;

}
}

最佳答案

您可以更改:

public double getSpeed()   {
return speed; //avg speed in knots
}

至:

  public String getSpeed()   {
return speed+" knots";
}

关于java - "The value of the field speed is the average speed in knots"添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28447558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com