public class FlightTest
{
public static void main(String[] args)
{
Flight info = new Flight(null, 0, null, null);
String name = "Delta Airlines";
int flightNum = 54663;
String origin = "Dallas, TX";
String destination = "Canton, MI";
info.setDestination(destination);
info.setFlightNum(flightNum);
info.setName(destination);
info.setOrigin(destination);
System.out.print(info);
}
}
上面的代码是我的驱动程序。
以下是我使用的方法。我不确定它是否被正确调用或命名不正确。
public class Flight {
private String name;
private int flightNum;
private String origin;
private String destination;
public Flight (String name, int flightNum, String origin, String destination) {
name = "";
flightNum = 0;
origin ="";
destination = "";
}
public enum Status {
ONTIME, DELAYED, CANCELLED;
}
private Status flightStatus = Status.ONTIME;
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public int getFlightNum() {
return flightNum;
}
public void setFlightNum(int newFlightNum) {
flightNum = newFlightNum;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String newOrigin) {
origin = newOrigin;
}
public String getDestination() {
return destination;
}
public void setDestination(String newDestination) {
destination = newDestination;
}
public String toString() {
return "The airline name is " +name+ ".\the flight number is " +flightNum+".\the origin is " + origin + ".\the destination is " +destination;
}
}
.toString()
在打印时切断了“ the”一词,对于除flightnum之外的所有内容,打印出来的都是“ can mi”。
答案 0 :(得分:1)
,除了flightnum以外,所有内容都显示为“ canton mi” 。
好吧,您正在将所有内容都设置到目标位置,即"canton, mi"
:
info.setDestination(destination); // destination
info.setFlightNum(flightNum);
info.setName(destination); // destination
info.setOrigin(destination); // destination
您需要使用正确的值填充info
:
info.setDestination(destination);
info.setFlightNum(flightNum);
info.setName(name);
info.setOrigin(origin);
此外,\t
只是一个选项卡,因此将其打印为空白。删除开头的\
。
答案 1 :(得分:0)
您的字符串中有\t
,这是一个转义字符。 \t
表示制表符,因此您的结果是
The airline name is Canton, MI.{tab}he
您应该删除\
。