无法正确显示一维数组输出
Trouble with Displaying 1D Array Output Correctly
我的程序包含几个选项,用于处理有关节目的用户输入数据(名称、日期、时间)。其中一个选项是显示数据和每天的总演出次数(例如:如果星期二有 2 场演出,它将显示 "There are 2 shows on Tuesday")。到目前为止,用于显示所有数据的输出都可以正常工作,但是在显示特定日期的节目数量时,它无法正常工作。我读过其他几个 java 程序,它们似乎每天都有一个 switch 语句,但也没有用。如果对我应该更改我的代码有什么建议,我将不胜感激!谢谢
我已经编辑了我之前的代码,但它仍然没有用
注:int dayCount放在enter data方法中;一天之后[i] = br.readLine();
这是我的 class:
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()
}
在打印总数的循环中显示您有:
for (i = 0; i < x; i++) {
if(i ==0) {
System.out.println("There is " + dayCount + " shows on " + day[i]);
}
}
if(i == 0)
使得 println 仅在循环的第一次迭代时执行。取出if语句有:
for (i = 0; i < x; i++) {
System.out.println("There is " + dayCount + " shows on " + day[i]);
}
这应该会打印出剩余的天数
此外,您只有一个 dayCount
变量可以用一整天。因此,无论演出在哪一天,您都会增加 dayCount
。 (如果你在不同的日子有三场演出,你仍然每次都会递增 dayCount
所以当你打印出来时它表明你每天有三场演出)如果你想为每个都有一个单独的 dayCount
变量一天,我推荐使用Hashmap
。
(使用日期作为键,dayCount
作为值)
您还可以查看 enums
以用于一周中的几天。
我的程序包含几个选项,用于处理有关节目的用户输入数据(名称、日期、时间)。其中一个选项是显示数据和每天的总演出次数(例如:如果星期二有 2 场演出,它将显示 "There are 2 shows on Tuesday")。到目前为止,用于显示所有数据的输出都可以正常工作,但是在显示特定日期的节目数量时,它无法正常工作。我读过其他几个 java 程序,它们似乎每天都有一个 switch 语句,但也没有用。如果对我应该更改我的代码有什么建议,我将不胜感激!谢谢
我已经编辑了我之前的代码,但它仍然没有用
注:int dayCount放在enter data方法中;一天之后[i] = br.readLine();
这是我的 class:
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()
}
在打印总数的循环中显示您有:
for (i = 0; i < x; i++) {
if(i ==0) {
System.out.println("There is " + dayCount + " shows on " + day[i]);
}
}
if(i == 0)
使得 println 仅在循环的第一次迭代时执行。取出if语句有:
for (i = 0; i < x; i++) {
System.out.println("There is " + dayCount + " shows on " + day[i]);
}
这应该会打印出剩余的天数
此外,您只有一个 dayCount
变量可以用一整天。因此,无论演出在哪一天,您都会增加 dayCount
。 (如果你在不同的日子有三场演出,你仍然每次都会递增 dayCount
所以当你打印出来时它表明你每天有三场演出)如果你想为每个都有一个单独的 dayCount
变量一天,我推荐使用Hashmap
。
(使用日期作为键,dayCount
作为值)
您还可以查看 enums
以用于一周中的几天。