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()被切断的话“中的”打印时,所有它打印出来是“密歇根州”以外flightnum的一切,这是正确的。
2 回答
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
除了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它只是一个选项卡,因此它被打印为空白区域。删除领先的\.
子衿沉夜
TA贡献1828条经验 获得超3个赞
您\t
的字符串中有一个转义字符。\t
意味着选项卡,所以你的结果是
The airline name is Canton, MI.{tab}he
您应该删除\
.
添加回答
举报
0/150
提交
取消