3

我的任务要求我制作一个电视节目节目,我可以在其中输入节目、删除、修改和排序节目。我坚持的是排序部分。程序提示用户输入节目名称、新剧集首映日期和时间,这些信息存储在数组中。它按键名、日期和时间(按字母顺序和数字顺序)排序。

程序提示用户输入其中一个键,然后程序需要按该键对节目进行排序(按天排序将按字母顺序排序)。

我做了一个类并使用一个数组来保存输入的节目。这是课程:

public class showInfo   
{  
String name;   
String day;   
int time;       
}

我输入了这样的节目:

public static void addShow() throws IOException
    {
    //initialize counter
    int i = 0;
    arr = new showInfo[i];

    showInfo temp = new showInfo();

    //input information
    do
    {
        System.out.print("Enter the name of show: ");
        String showName = br.readLine();
        temp.name = showName;

        System.out.print("Enter which day of the week a new episode premieres: ");
        String showDay = br.readLine();
        temp.day = showDay;

        System.out.print("Enter time in 24-hour format (e.g. 2100, 1900): ");
        int showTime = Integer.valueOf(br.readLine()).intValue();
        temp.time = showTime;

        i++;

        System.out.print("Would you like to add another show? (y/n) ");
    }
    while((br.readLine().compareTo("n"))!=0);
}

为了按时间排序,我编写了以下方法:

public static void timeSort()
{
    int min;
    for (int i = 0; i < arr.length; i++) 
    {

        // Assume first element is min
        min = i;
        for (int j = i+1; j < arr.length; j++) 
        {
            if (arr[j].time < arr[min].time) 
            {
                min = j;
            }
        }

        if (min != i) 
        {
            int temp = arr[i].time;
            arr[i].time = arr[min].time;
            arr[min].time = temp;
        }
    }
    System.out.println("TV Shows by Time");
    for(int i = 0; i < arr.length; i++)
    {
        System.out.println(arr[i].name + " - " + arr[i].day + " - " + arr[i].time + " hours");
    }
}

我的问题是,当我调用它并在主中输出它时,它只显示“按时间显示的电视节目”文本,但不显示节目。为什么是这样?(我应该显示我的完整代码吗?)

我认为我的问题与计数器有关,但我不确定我将如何解决这个问题,因为用户应该输入尽可能多的节目,并且节目的数量在其他中被修改和删除方法。

任何帮助都会很棒!提前致谢!

4

2 回答 2

2

如果不禁止您使用 Java 的 Collections API,请使用 Arrays.sort(T[],Comparator)。然后,您的程序可以维护三个静态 Comparator 对象——一个用于表演,一个用于一天,一个用于时间。例如,

  static Comparator<showTime> daySort = new Comparator<showTime>(){
    public int compare(showTime st1, showTime st2){
      return st1.day.compareTo(st2.day);
    }
    public boolean equals(Object o){
      return this==o; /* Easy way out for this method. */
    }
  };

然后你有一个调用 Arrays.sort 的排序方法(如果它甚至是必要的):

  public static void sort(){
    Arrays.sort(arr, daySort);
  }

有关详细信息,请参阅数组比较器。

于 2013-09-14T18:33:03.807 回答
2

我认为排序不起作用,因为ShowInfo[]数组中没有要排序的元素。

您正在方法ShowInfo内初始化数组addShow

//initialize counter
int i = 0;
arr = new showInfo[i];

你正在创建一个新的ShowInfo

ShowInfo temp = new showInfo(); 

但这temp从未分配给任何数组元素。所以,每次你调用这个方法来添加一个新的show,你实际上是在创建一个ShowInfo[]长度为 0 的新数组,并且它的一个实例ShowInfo从未分配给任何数组元素。

您只需初始化数组/计数器一次,因此将数组/计数器initialization代码与其使用分开(您不应将这些初始化语句保留在任何删除/修改/排序方法中):

private static int i=0;
private static ShowInfo[] arr=new ShowInfo[NO_OF_SHOWINFOS];

有了这些,你的addShow方法就会变成这样:

public static void addShow() throws IOException
    {
    //initialize counter
    //int i = 0;
    //arr = new showInfo[i];

    //showInfo temp = new showInfo();

    //input information
    do
    {
        showInfo temp = new showInfo();

        System.out.print("Enter the name of show: ");
        String showName = br.readLine();
        temp.name = showName;

        System.out.print("Enter which day of the week a new episode premieres: ");
        String showDay = br.readLine();
        temp.day = showDay;

        System.out.print("Enter time in 24-hour format (e.g. 2100, 1900): ");
        int showTime = Integer.valueOf(br.readLine()).intValue();
        temp.time = showTime;

        //i++;
        arr[i++]=temp;
        System.out.print("Would you like to add another show? (y/n) ");
    }
    while((br.readLine().compareTo("n"))!=0);
}

}

于 2013-09-15T02:35:59.483 回答