[英]How to fix this programming code? (MOOCFI java week7 exercise 8)
我正在關注 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("");
}
}
}
飛機.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;
}
}
飛行.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 + ")";
}
}
主程序
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.
}
}
我想弄清楚兩個問題:
System.out.print("Choose operation:\n[1] Add airplane" + "\n[2] Add flight\n[x] Exit\n> ");
這是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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.