我正在遵循Moocfi练习8(Java在线课程),但是无法调试我的编程代码。
https://materiaalit.github.io/2013-oo-programming/part2/week-7/(我要问的问题在页面的底部,名为“机场”)
机场.java
import java.util.Scanner;
import java.util.HashMap;
import java.util.ArrayList;
public class Airport {
private HashMap<Plane, Flight> num;
private Scanner read;
private ArrayList<Plane> planes;
public Airport(){
read = new Scanner(System.in);
this.num = new HashMap<Plane, Flight>();
planes = new ArrayList<Plane>();
}
public void start(){
while(true){
System.out.print("Choose operation:\n[1] Add airplane" + "\n[2] Add flight\n[x] Exit\n> ");
String ans = read.nextLine();
if (ans.equals("1")){
num1();
} else if (ans.equals("2")){
num2();
} else if (ans.equals("x")){
break;
} else continue;
}
System.out.println("\nFlight service\n------------\n");
while(true){
System.out.print("Choose operation:\n[1] Print planes\n"
+ "[2] Print flights\n[3] Print plane info\n[x] Quit\n> ");
String ans2 = read.nextLine();
if (ans2.equals("1")){
nus1();
}
else if (ans2.equals("2")){
nus2();
}
else if (ans2.equals("3")){
nus3();
}
else if (ans2.equals("x")){
break;
}
else continue;
}
}
public void num1(){
System.out.print("Give plane ID: ");
String pi = read.nextLine();
System.out.print("Give plane capacity: ");
int pin = read.nextInt();
planes.add(new Plane(pi, pin));
System.out.println("");
}
public void num2(){
System.out.print("Give plane ID: ");
String pi = read.nextLine();
System.out.print("Give departure airport code: ");
String pin = read.nextLine();
System.out.print("Give destination airport code: ");
String pim = read.nextLine();
for (Plane a:planes){
if (a.getName()==pi){
Plane b = new Plane(a.getName(), a.getCap());
num.put(b, new Flight(pin, pim));
}
}
System.out.println("");
}
public void nus1(){
for (Plane p: planes){
System.out.println(p.getName() + " (" + p.getCap() + " ppl)");
}
System.out.println("");
}
public void nus2(){
for (Plane p : num.keySet()){
System.out.println(p.getName() + " (" +
p.getCap() + " ppl) " + num.get(p));
}
System.out.println("");
}
public void nus3(){
System.out.print("Give plane ID: ");
String b = read.nextLine();
for (Plane a: planes){
if (a.getName().equals(b)){
System.out.println(a.getName() + " (" + a.getCap() + " ppl)");
}
System.out.println("");
}
}
}
Plane.java
public class Plane {
private String name;
private int capacity;
public Plane(String name, int capacity){
this.name = name;
this.capacity = capacity;
}
public String getName(){
return name;
}
public int getCap(){
return capacity;
}
}
Flight.java
public class Flight {
private String departureCode;
private String destinationCode;
public Flight(String departureCode, String destinationCode){
this.departureCode = departureCode;
this.destinationCode = destinationCode;
}
public String toString(){
return "(" + departureCode + "-" + destinationCode + ")";
}
}
Main.java
public class Main {
public static void main(String[] args) {
Airport a = new Airport();
a.start();
// Write your main program here. Implementing your own classes will be useful.
}
}
我要解决两个问题:
当我运行该程序并按1并保存飞机信息时,它随后将以下语句打印两次,应该只打印一次。
System.out.print("Choose operation:\n[1] Add airplane" + "\n[2] Add flight\n[x] Exit\n> ");
另外,在Airport.java上,我将航班信息保存在HashMap变量中,以便以后打印出来,但是当我运行该程序时,它似乎不起作用。
最佳答案
这是nextInt()
的问题,按Enter键时将不考虑换行符。使用nextLine()
并将其强制转换为int
在num1()
方法中,替换
int pin = read.nextInt();
与
int pin = Integer.parseInt(read.nextLine());
遇到您的
HashMap
问题,您正在使用==
比较字符串。使用equals
在
num2()
方法中,替换if (a.getName() == pi)
与
if (a.getName().equals(pi))