程序中没有输出

时间:2017-10-29 23:45:32

标签: java

一堆城市存储在一个对象中,这些城市由ArrayList结构排列。

我试图运行“TrainTimeTable”,这是代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;

public class TrainTimeTable {
private LinkedList<Station> schedule;

public void delay(String station, int minutes) {
    boolean startDelay = false;
    ListIterator<Station> sIt = schedule.listIterator();
    while (sIt.hasNext()) {

        Station currStation = sIt.next();
        if (!startDelay && currStation.getCity().equals(station)) {
            startDelay = true;
        }
        if (startDelay) {
            currStation.delay(minutes);
        }
    }
}

public String getCities() {
    String cities = "";
    for (Station ast : schedule) {
        cities += ast.getCity() + "\n";
    }
    return cities;
}

public void displaySchedule() {
    System.out.printf("%16s\t%8s\t%8s\t%3s\n", "Station", "Arrival",
            "Departure", "Day");
    for (int i = 0; i < schedule.size(); i++) {
        schedule.get(i).displaySchedule();
    }
}

public TrainTimeTable(LinkedList<Station> schedule) {
    super();
    this.schedule = schedule;
}

public static void main(String[] args) throws ParseException {
    LinkedList<Station> schedule = new LinkedList<Station>();
    SimpleDateFormat sdf = new SimpleDateFormat("d HH:mm");
    schedule.add(new Station(null, sdf.parse("1 20:30"), "Vancouver"));
    schedule.add(new Station(sdf.parse("2 06:00"), sdf.parse("2 06:35"),
            "Kamloops"));
    schedule.add(new Station(sdf.parse("2 16:00"), sdf.parse("2 17:30"),
            "Jasper"));
    schedule.add(new Station(sdf.parse("2 23:00"), sdf.parse("2 23:59"),
            "Edmonton"));
    schedule.add(new Station(sdf.parse("3 08:00"), sdf.parse("3 08:25"),
            "Saskatoon"));
    schedule.add(new Station(sdf.parse("3 20:45"), sdf.parse("3 22:30"),
            "Winnipeg"));
    schedule.add(new Station(sdf.parse("4 05:02"), sdf.parse("4 05:42"),
            "Sioux Lookout"));
    schedule.add(new Station(sdf.parse("4 15:35"), sdf.parse("4 16:10"),
            "Hornepayne"));
    schedule.add(new Station(sdf.parse("5 00:18"), sdf.parse("5 00:48"),
            "Capreol"));
    schedule.add(new Station(sdf.parse("5 09:30"), null, "Toronto"));

    TrainTimeTable ttt = new TrainTimeTable(schedule);
    Scanner inp = new Scanner(System.in);
    String cmd = "";
    while (!cmd.equalsIgnoreCase("Quit")) {
        System.out.println("Please input command [Quit | Delay | Show]:");
        cmd = inp.next();
        if (cmd.equalsIgnoreCase("Show")) {
            ttt.displaySchedule();
        } else if (cmd.equalsIgnoreCase("Delay")) {
            System.out.println("Please input Station to delay:");
            String station = inp.next();
            System.out.println("Please input Minutes to delay:");
            int minutes = inp.nextInt();
            ttt.delay(station, minutes);
        }
    }
    inp.close();
}

}

与“Station”一起

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Station {
private Date arrival;
private Date departure;
private String city;

/**
 * @param arrival
 * @param departure
 * @param city
 */
public Station(Date arrival, Date departure, String city) {
    super();
    this.arrival = arrival;
    this.departure = departure;
    this.city = city;
}

public String getArrival() {
    if (arrival == null)
        return "";
    return new SimpleDateFormat("HH:mm").format(arrival);
}

/**
 * @param arrival
 *            the arrival to set
 */
public void setArrival(Date arrival) {
    this.arrival = arrival;
}

public int getDate() {
    Calendar cal = new GregorianCalendar();
    if (departure == null)
        cal.setTime(arrival);
    else
        cal.setTime(departure);

    return cal.get(Calendar.DAY_OF_YEAR);
}

public void displaySchedule() {
    System.out.printf("%16s\t%8s\t%8s\t%3d\n", getCity(), getArrival(),
            getDeparture(), getDate());
}

public void delay(int minutes) {

    Calendar cal = new GregorianCalendar();
    if (arrival != null) {
        cal.setTime(arrival);
        cal.add(Calendar.MINUTE, minutes);
        arrival = cal.getTime();
    }
    if (departure != null) {
        cal.setTime(departure);
        cal.add(Calendar.MINUTE, minutes);
        departure = cal.getTime();
    }
}

public String getDeparture() {
    if (departure == null)
        return "";
    return new SimpleDateFormat("HH:mm").format(departure);
}

/**
 * @param departure
 *            the departure to set
 */
public void setDeparture(Date departure) {
    this.departure = departure;
}

/**
 * @return the city
 */
public String getCity() {
    return city;
}

/**
 * @param city
 *            the city to set
 */
public void setCity(String city) {
    this.city = city;
}

}

我使用Netbeans作为IDE,没有显示任何错误,但我不明白为什么它没有运行。我觉得我看起来很小。

1 个答案:

答案 0 :(得分:0)

如果要在命令行中编译,请在项目的根目录中使用:

进行编译
javac -d bin -sourcepath src src/TrainTimeTable.java

我承认你把文件放在文件夹src中,你有一个文件夹bin。

现在,您可以进入文件夹bin并运行

java TrainTimeTable

注1:src是包含所有* .java文件的文件夹。

注2:bin或out或target是包含所有* .class文件的文件夹。