我的任务要求我制作一个电视节目节目,我可以在其中输入节目、删除、修改和排序节目。我坚持的是排序部分。程序提示用户输入节目名称、新剧集首映日期和时间,这些信息存储在数组中。它按键名、日期和时间(按字母顺序和数字顺序)排序。
程序提示用户输入其中一个键,然后程序需要按该键对节目进行排序(按天排序将按字母顺序排序)。
我做了一个类并使用一个数组来保存输入的节目。这是课程:
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");
}
}
我的问题是,当我调用它并在主中输出它时,它只显示“按时间显示的电视节目”文本,但不显示节目。为什么是这样?(我应该显示我的完整代码吗?)
我认为我的问题与计数器有关,但我不确定我将如何解决这个问题,因为用户应该输入尽可能多的节目,并且节目的数量在其他中被修改和删除方法。
任何帮助都会很棒!提前致谢!