【发布时间】:2013-09-14 18:14:15
【问题描述】:
我的任务要求我制作一个电视节目节目,我可以在其中输入节目、删除、修改和排序节目。我坚持的是排序部分。该程序提示用户输入节目名称、新剧集首映日期和时间,这些信息存储在数组中。它按键名、日期和时间(按字母顺序和数字顺序)排序。
程序提示用户输入其中一个键,然后程序需要按该键对节目进行排序(按天排序将按字母顺序排序)。
我创建了一个类并使用了一个数组来保存输入的节目。这是课程:
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");
}
}
我的问题是,当我调用它并在主中输出它时,它只显示“按时间显示的电视节目”文本,而不显示节目。为什么是这样? (我应该显示我的完整代码吗?)
我认为我的问题与计数器有关,但我不确定如何解决这个问题,因为用户应该输入任意数量的节目,并且节目数量已被修改用其他方法删除。
任何帮助都会很棒!提前致谢!
【问题讨论】:
-
你需要告诉我们你在哪里以及如何填充你的数组。
-
您是否验证过您的数组长度 > 0...或者您确定在调用该方法时该数组中确实有值?
-
显示准确的输出总是有帮助的。如果您没有看到“- - - - 小时”...
-
在timeSort方法开始处添加System.out.println("展示次数="+arr.length);验证您确实有一些节目。
-
看起来您只是在交换时间,而不是交换数组中的条目。应该是:showInfo temp = arr[i]; arr[i]=arr[分钟]; arr[min]=温度;