私の課題は、番組を入力し、削除し、修正し、並べ替えることができるテレビ番組プログラムを作成することです。私が立ち往生しているのは、ソート部分です。このプログラムは、番組の名前、新しいエピソードが初公開される日、および時間をユーザーに入力するように求めます。これらは配列に格納されています。キーの名前、曜日、時間 (アルファベット順および数字順) で並べ替えられます。
プログラムはユーザーにそれらのキーの 1 つを入力するように促し、プログラムはそのキーで番組を並べ替える必要があります (日で並べ替えるとアルファベット順に並べ替えられます)。
クラスを作成し、配列を使用して入力されたショーを保存しました。クラスは次のとおりです。
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");
}
}
私の問題は、それを呼び出してメインで出力すると、「TV Shows by Time」テキストのみが表示され、番組は表示されないことです。どうしてこれなの?(完全なコードを表示する必要がありますか?)
私の問題はカウンターと関係があると思いますが、ユーザーは好きなだけショーを入力する必要があり、ショーの量は他の方法で変更および削除されるため、それを修正する方法がわかりませんメソッド。
どんな助けでも素晴らしいでしょう!前もって感謝します!