java - 使用 LinkedList Java 的空中交通管制程序出现问题

标签 java linked-list implementation

我已经在这个程序上工作了几个星期,这是我们在 Java 编程课上的最后一个项目,它一直给我(和许多其他学生)带来一些非常头痛的问题。 我们需要创建一个程序,允许用户输入、显示和删除新飞机以及飞机的速度、高度和类型。 我在让主类与其他类进行通信方面遇到了最多的问题。因此,我不知道我的 LinkedList 是否能够正常工作,或者是否能够正常工作。我担心列表不会正确地将所有字段存储在一起,并且节点未正确编码。 我真的需要您提供的任何帮助或建议。代码如下。我愿意接受任何建议。代码不必保留在与当前所在的完全相同的类中。如果某些东西在其他地方可以更好地工作,我很乐意尝试它。

主类。这是用户与程序交互的地方。我一直很难让其他类(class)的方法在这个类(class)中发挥作用。我确信我缺少的东西很简单。

package airTraffic;

import java.util.*;

public class Main {

static Scanner in = new Scanner(System.in);

public static void main(String[] args) {

    do {
        try {
            System.out.println("Please enter command to proceed: ");
            System.out.println("Enter new aircraft = e");
            System.out.println("Display all aircraft = d");
            System.out.println("Show specific flight = s");
            System.out.println("Remove specific flight = r");
            String command = in.next();
            in.next(command);

            if ( (in.next(command)).equals("e") ) {
                ATControl.addToList();  // need to somehow "start" this class 

            } else if ( (in.next(command)).equals("d") ) {
                ATControl.displayAll();

            } else if ( (in.next(command)).equals("s") ){
                ATControl.showFlight();

            } else if ( (in.next(command)).equals("r") ) {
                ATControl.removeFlight();

            } else if ( (in.next(command)).equals(null) ) {
            }
        } catch (InputMismatchException exc) {
            System.out.println("Wrong entry, please try again:");
        }
    } while (true);
}
}

链表和节点 - 我称之为飞机。我认为这是存储和创建列表的地方。对列表的操作发生在下一类(ATControl)中,或者至少我认为会发生。

package airTraffic;

import java.util.LinkedList;

public class Aircraft  {

// stores data
private static final int INITIAL_ALLOCATION = 20;
private int size = INITIAL_ALLOCATION; 

//declare LinkedList and node names
static LinkedList <String> list = new LinkedList <String> ();
private Aircraft head = new Aircraft ();
private Aircraft tail = new Aircraft ();

// tells list to add nodes
public  void addNodes (int n, LinkedList<String> s) {
    s = list;
    head.next = tail;
    tail.next = tail;
    size = n;
    Aircraft temp = head;
    for (int i= 0; i < size; ++i) {
        temp.next = new Aircraft ();
        temp = temp.next;
    }
    temp.next = tail;
}

private String value;
Aircraft craft;

public Aircraft (String v) {
    value = v;
}

public Aircraft () {

}

public String get () {
    return value;
}

public void set (String v) {
    value = v;
}

public Aircraft next = null;

//auto generated method from ATControl 
public static void add(String flight) {
    // a for or while loop might be needed here. Seems to easy to just have an empty add class

}
//auto generated method from ATControl
public static void remove() {

}
 }

ATControl 类。 (我认为)这是操纵列表的地方,允许用户添加、删除和显示航类。

package airTraffic;

import java.util.*;

public class ATControl{

// implement Aircraft class (node) - empty argument list?? 
Aircraft aircraft = new Aircraft ();

static Scanner in = new Scanner (System.in);

// list of planes 
static String [] planeList = {"Wide-body Airliner = w", "Regional Airliner = r", "Private Plane = p", 
        "Military = m", "Cargo only: c", "Unknown = u"};

//add plane and details
public static void addToList () {
    System.out.printf("Enter flight number: ");
    String flight = in.nextLine();
    Aircraft.add(flight);

    //type of plane
    System.out.printf("Enter type of plane, ", "Choose from: " + planeList);
    String type = in.nextLine();
    try {
    if (type == "w") {
         System.out.println("Wide-body Airliner");
    }else if (type == "r") {
             System.out.println("Regional Airliner");
    }else if (type == "p") {
         System.out.println("Private Plane");
    }else if (type == "m") {
         System.out.println("Military");
    }else if (type == "c") {
         System.out.println("Cargo only");
    }else if (type == "u") {
         System.out.println("Unknown");
    } else type = null;
        }
    catch (InputMismatchException i) {
        System.out.println("You must enter valid command: " + planeList);
    }
    Aircraft.add(type);

    //plane speed
    System.out.printf("Enter current speed: ");
    String speed = in.nextLine();
    Aircraft.add(speed);

    //add Altitude 
    System.out.printf("Enter current altitude: ");
    String alt = in.nextLine();
    Aircraft.add(alt);
}

//show flight
public static void showFlight () {
    System.out.printf("Enter flight number for details: ");
    in.nextLine();
    Aircraft.get(Aircraft, index);
}

// display all flights
public static void displayAll () {
    System.out.printf("All flights: " );

}

//remove flight
public static void removeFlight () {
    System.out.printf("Enter flight number to be removed: ");
    in.nextLine();
    Aircraft.remove();
}   
}

有什么想法吗?谢谢你!

最佳答案

要“启动”ATControl,您需要创建它的新实例:

ATControl control = new ATControl();
control.addToList();

与您的飞机相同。您将需要使用 new 创建新实例,然后对它们调用 Add() 等。

您可能还希望将 Scanner 从 main 传递到新的 ATControl 并使用它来读取输入,而不是使用全新的 Scanner

关于java - 使用 LinkedList Java 的空中交通管制程序出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11726162/

相关文章:

java - 使用 Redis 进行任务调度

c++ - C++中的高效链表?

algorithm - 为链表插入伪代码

interface - Rust语言接口(interface)实现的递归调用

java - 实体类应该实现接口(interface)吗?

java - JSTL foreach - 如何从最后一个循环到第一个

java - Android takePicture 不调用回调方法 onPictureTaken()

language-agnostic - 链表在什么情况下有用?

c++ - 源文件中的 Typedef 私有(private)结构原型(prototype)

java - 错误 : Java: invalid target release: 11 - IntelliJ IDEA