【发布时间】:2018-11-15 00:02:41
【问题描述】:
我的程序包含几个选项,用于处理用户输入的节目数据(名称、日期、时间)。其中一个选项是显示数据和每天的节目总数(例如:如果周二有 2 场演出,则会显示“周二有 2 场演出”)。到目前为止,显示所有数据的输出都可以正常工作,但是在显示特定日期的节目数量时,它无法正常工作。我已经阅读了其他几个似乎每天都有 switch 语句的 java 程序,但也没有工作。如果对我应该对我的代码进行哪些更改有任何建议,我将不胜感激!谢谢
我已经编辑了我之前的代码,但它仍然没有工作
注意:int dayCount 放在输入数据方法中;日后[i] = br.readLine();
这是我的课:
import java.io.*;
public class Javavision {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static String name[] = new String[1000];
static String day[] = new String[1000];
static String time[] = new String[1000];
static int dayCount = 0;
static int x, i, j, smallest;
static String temp;
这是我的代码:
public static void showShows() {
//output all shows
for (i = 0; i < x; i++) {
System.out.println("Name : " + name[i]);
System.out.println("Day : " + day[i]);
System.out.println("Time(2:30 am = 0230) : " + time[i] + "\r");
} **The problem is here**
for (i = 0; i < x; i++) {
if(i ==0) {
System.out.println("There is " + dayCount + " shows on " + day[i]);
}
}
}
这是输出:
Name : The Flash
Day : Sunday
Time(2:30 am = 0230) : 0125
Name : Suits
Day : Sunday
Time(2:30 am = 0230) : 0450
Name : Java Program
Day : Tuesday
Time(2:30 am = 0230) : 0330
There is 3 shows on Sunday
这是我增加 dayCount 的地方:
//Method addShow
public static void addShow() throws IOException {
//initialize counter
x = 0;
do {
//Update Array
System.out.println("Enter Name of Show: ");
name[x] = in.readLine();
System.out.println("Enter Day of Show: ");
day[x] = in.readLine();
dayCount++;
System.out.println("Enter Time of Show (ex: 2:30am = 0230) : ");
time[x] = in.readLine();
//Increase counter
x++;
//Ask if the user wants to stop
System.out.println("\nTo continue press Enter ");
System.out.println("To Quit, type in 'quit': ");
}
while((in.readLine().compareTo("quit"))!=0);
//Method addShow()
}
【问题讨论】:
-
你在哪里增加
showdayCount中的任何元素? -
抱歉,我好像忘记了。我已经对其进行了更改,但它仍然没有工作。我有一种预感,我应该每天在 switch 语句中进行初始化,但我不知道该怎么做。
-
所以你的问题是你周日只有两场演出,但最后输出的是三场?
-
是的,它没有正确显示每天的总节目。例如,输出应该显示“周日有 2 场演出”,下一行显示“周二有 1 场演出”。
-
能否请您张贴您增加
showdayCount的代码?
标签: java arrays output display