0

我正在使用 ArrayList 来保存节目的名称、日期和时间。我的程序要求我输出一周中每一天播放的节目数量。我输入了 4 个不同的节目,其中两个在同一天。到目前为止,输出给我的唯一信息是“星期四有/是 2 个节目”。另外两个没有出现。我怎样才能让它显示我输入的每一天的节目数量?这是我的代码:

    String showDay = ((showInfo)show.get(0)).day.toString();
    int totalShows = 0;

    //print occurences for how many shows play each day
    for (int i = 0; i < show.size(); i++) {
           if (showDay.equals(((showInfo)show.get(i)).day.toString())) {
                totalShows++;
           }
      }

      System.out.println("On " + showDay + " there are/is " + totalShows + " show(s).");
 }

这是我输入的节目的代码:

//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;

        show.add(temp);            

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

请记住,我使用的是 Java 1.4。没有其他选择。应老师要求。

这可能很明显,但我现在忘记了。任何帮助都会很棒!提前致谢!

编辑:

String showDay = ((showInfo)show.get(0)).day.toString();

    if("sunday".equalsIgnoreCase(showDay)){
        showdayCount[0]++;
        System.out.println("There are/is " + showdayCount[0]++ + " show on Sunday.");
    }else if("monday".equalsIgnoreCase(showDay)){
        showdayCount[1]++;
        System.out.println("There are/is " + showdayCount[1]++ + " show on Monday.");
    }else if("tuesday".equalsIgnoreCase(showDay)){
        showdayCount[2]++;
        System.out.println("There are/is " + showdayCount[2]++ + " show on Tuesday.");
    }else if("wednesday".equalsIgnoreCase(showDay)){
        showdayCount[3]++;
        System.out.println("There are/is " + showdayCount[3]++ + " show on Wednesday.");        
    }else if("thursday".equalsIgnoreCase(showDay)){
        showdayCount[4]++;
        System.out.println("There are/is " + showdayCount[4]++ + " show on Thursday.");
    }else if("friday".equalsIgnoreCase(showDay)){
        showdayCount[5]++;
        System.out.println("There are/is " + showdayCount[5]++ + " show on Friday.");
    }else if("saturday".equalsIgnoreCase(showDay)){
        showdayCount[6]++;
        System.out.println("There are/is " + showdayCount[6]++ + " show on Saturday.");
    }

这也只给了我一行。我究竟做错了什么?!:(

编辑:

我想要的是输入电视节目的名称/日期/时间,然后能够显示该特定日期的节目数量!

例如,生活大爆炸和社区都在星期四。所以代码会输出类似

There are 2 shows on Thursday.

到目前为止,什么都没奏效。

4

2 回答 2

0

你只能从这条线上得到一天:

String showDay = ((showInfo)show.get(0)).day.toString(); 

这是我的 hack 解决方案(可能有一些错误,例如命名错误的变量,但您应该能够修复它):

 //Create a map that maps days to the shows that are on that day
 LinkedHashMap showDays=new LinkedHashMap(); //String to Set map
 String[] days={"Sunday","Monday", ....};//put the rest of the days in here
 for(int dayIndex=0;dayIndex<days.length;dayIndex++){
     String day=days[dayIndex];
     showDays.put(day,new HashSet());
 }

 //iterate through the shows and put them in their respective days in the map
 for (int i = 0; i < show.size(); i++) {
     String dayForShow=((showInfo)show.get(i)).day.toString();
     showDays.get(dayForShow).put(show.get(i));
 }

 //now print out how many shows are on each day
 for(int dayIndex=0;dayIndex<days.length;dayIndex++){
     String day=days[dayIndex];
     int showsToday=showDays.get(day).size();
    ///do your print out now
 }

您可以将“LinkedHashMap showDays”设为类变量,并在每次添加到显示时添加到它(每次从显示中删除时从其中删除)。

PS。告诉你的老师,现在已经不是 2002 年了,技术在进步,它们让我的工作变得更加困难。

于 2013-11-03T00:56:07.243 回答
0

创建一个Show

public class Show {
    String showName;
    String showDay;
    int showTime;
    static int[] showdayCount = new int[7];

    public Show(String showName, String, showDay, iny showTime){
        this.showName = showName;
        this.showDay = showDay;
        this.showTime = showTime;

        // increment showDayCOunt[] according to showDay from 
        switch(showDay){
            case "Sunday": showDayCount[0]++; break;
            case "Monday": showDayCount[1]++; break;
            case "Tuesday": showDayCount[2]++; break;
            case "Wednesday": showDayCount[3]++; break;
            case "Thursday": showDayCount[4]++; break;
            case "Friday": showDayCount[5]++; break;
            case "Saturday": showDayCount[6]++; break;

        }

    }
}

这是 YourClass 的主要方法

public static void main(String[] args){
    // Create Array of Shows
    Show[] shosList = new Show[4]; // or how many ever shows you have

    // Get your inputs for showName, showDay, showTime
    Sting showName = 
    String showDay = 
    int showTime = 


    // Create show Object
    Show show = new Show(showName, showDate, showTime);

    // add show to ArrayList
    showList[someIndex] = show;

    // Do all other stuff then print
    // when you print, you can get the showDayCount directly from the Show class

}

现在我不想为你做所有事情。我只是想让你朝着正确的方向前进。请注意,如果您希望上述情况多次发生,请考虑将其放入循环中。

编辑:使用 getShowdayCount

将此添加到我上面的 Show 课程中

public int getShowdayCount(String showday){
    switch(showDay){
        case "Sunday": return showDayCount[0]; break;
        case "Monday": return showDayCount[1]; break;
        case "Tuesday": return showDayCount[2]++; break;
        case "Wednesday": return showDayCount[3]++; break;
        case "Thursday": return showDayCount[4]++; break;
        case "Friday": return showDayCount[5]++; break;
        case "Saturday": return showDayCount[6]++; break;

    }
}

编辑:if/else ifswitch语句为例

// for main method inputs
if ("sunday"equalsIgnoreCase(showDay){
    showdayCount[0]++
} else ("monday".equalsIgnoreCase(showDay){
    showdayCount[1]++
} // finish if/else if statement


// for getShowdayCount method
if ("sunday"equalsIgnoreCase(showDay){
    return showdayCount[0];
} else ("monday".equalsIgnoreCase(showDay){
    return showdayCount[1];
} // finish if/else if statement

您可以main像这样从您的班级中调用它

show.getShowdayCount(show.showday);

编辑:main实际上,只需使用方法将上述编辑放入您的类中

YourClassWithMainMethod{

    static int[] showdayCount = new int[7];        

    public static void mina(String[] args){

        // call getShowdayCount here
        getShowdayCount(String showday);
    }

    public static int getShowdayCount(String showday){

    }
}

编辑:你想要什么和你不想要什么

你不需要这个 if 语句

 System.out.println("There are/is " + showdayCount[5]++ + " show on Friday.");

当您要打印所有节目时:

for (int i = 0; i < showdayCount.length; i++){
    int showday;

    if (i == 0){
       showday = "Sunday
       finish the if/else if statements
    }
    System.out.println("There is " + showDayCount[i] + " shows on " + showday);

}
于 2013-11-03T00:58:40.687 回答