java - 如何修复此编程代码? (MOOC FI java 第 7 周练习 8)

标签 java

我正在学习 Moocfi 练习 8(java 在线类(class)),但无法调试我的编程代码。 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 + ")";
    }
}

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. 当我运行该程序并按 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 问题,您正在使用 == 比较字符串。使用等于

    num2()方法中,替换

    if (a.getName() == pi)
    

    if (a.getName().equals(pi))
    

    关于java - 如何修复此编程代码? (MOOC FI java 第 7 周练习 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534520/

    相关文章:

    java - outputStream 关​​闭会导致其他 Streams 阻塞吗?

    java - 如何制作精度达到百分之一秒的秒表?

    java - 在运行时重新定义嵌套类,可能吗? (java.lang.NoClassDefFoundError : class names don't match)

    java - 在递归算法中计算运行时间

    java - 无法使用 C++ TAO 客户端连接到 java 1.3 NamingService

    java - 从 javafx 应用程序制作 jar 文件的正确方法

    java - PrintWriter 未写入文件。[Java]

    java 缓冲图像 : Detecting black pixels

    java - 不使用 .split() 或 StringTokenizer 在用户内容中查找单词

    java - org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject,org.apache.maven.archiver.MavenArchiveConfiguration)