我创建了一个程序来接受来自用户的输入,并将存储在2d数组中。示例情况下,用户输入的输入号为学期= 2;那么no当然也= 2;我的循环工作正常,但是数据不能很好地存储。
输出:零零零input4
应该是这样的:input1input2input3input4
下面是我的代码:
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter no of semester : ");
data = inData.readLine();
noSem = Integer.parseInt(data);
// Semester loop
for(int i = 0; i < noSem; i++) {
System.out.println("ntSemester" + (i + 1));
System.out.print("Enter number of course : ");
data = inData.readLine();
noCourse = Integer.parseInt(data);
// course details loop
for(int u = 0; u < noCourse; u++) {
System.out.println("Enter Course Code : ");
System.out.print("Enter Course Name : ");
sbjName = new String[noSem][noCourse];
sbjName[i][u] = inData.readLine();
System.out.println("Enter Credit Hour : ");
System.out.println("Enter Marks : ");
System.out.println("n");
}
}
for(int x = 0; x < noSem; x++) {
for(int y = 0; y < noCourse; y++) {
System.out.println(sbjName[x][y]);
}
}
}
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter no of semester : ");
int noSem = Integer.parseInt(inData.readLine());
String[][] sbjName = new String[noSem][];
// Semester loop
for (int i = 0; i < noSem; i++) {
System.out.println("ntSemester" + (i + 1));
System.out.print("Enter number of course : ");
int noCourse = Integer.parseInt(inData.readLine());
sbjName[i] = new String[noCourse];
// course details loop
for (int u = 0; u < noCourse; u++) {
System.out.println("Enter Course Code : ");
System.out.print("Enter Course Name : ");
sbjName[i][u] = inData.readLine();
System.out.println("Enter Credit Hour : ");
System.out.println("Enter Marks : ");
System.out.println("n");
}
}
for (int x = 0; x < sbjName.length; x++) {
for (int y = 0; y < sbjName[x].length; y++) {
System.out.println(sbjName[x][y]);
}
}
}
你的问题在这里:
sbjName = new String[noSem][noCourse];
语句在每次迭代期间创建一个新数组。换句话说:你丢掉了之前创建的数组;因此,之前存储在这些数组中的任何信息…也被扔掉了!
你只希望调用一次;所以你把移到循环的之外!
问题似乎是每次执行第一个for循环时都要重新创建2d数组。
要解决这个问题,您需要在开始时定义可用的课程数量:
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
String[][] sbjName = null;
String data;
int noSem = 2;
int noCourse = 3; // say there are 3 courses that are available
try {
System.out.print("Enter no of semester : ");
data = inData.readLine();
noSem = Integer.parseInt(data);
System.out.print("Enter number of courses available : ");
data = inData.readLine();
noCourse = Integer.parseInt(data);
sbjName = new String[noSem][noCourse];
// Semester loop
for (int i = 0; i < noSem; i++) {
System.out.println("ntSemester" + (i + 1));
System.out.print("Enter number of course : ");
data = inData.readLine();
int courseNum = Integer.parseInt(data);
// course details loop
for (int u = 0; u < courseNum; u++) {
System.out.println("Enter Course Code : ");
System.out.print("Enter Course Name : ");
sbjName[i][u] = inData.readLine();
System.out.println("Enter Credit Hour : ");
System.out.println("Enter Marks : ");
System.out.println("n");
}
}
for (int x = 0; x < noSem; x++) {
for (int y = 0; y < noCourse; y++) {
System.out.println(sbjName[x][y]);
}
}
} catch (Exception e) {
}