-2
import java.util.*;
public class Student
{
static CollegeCourse[] theirCourses = new CollegeCourse[5];
public static void main(String[] args)
{
int ID;
Scanner input = new Scanner(System.in);
/////////////////////////////////////////////////// Assigns student ID
System.out.println("Please enter your student ID >>");
ID = input.nextInt();
///////////////////////////////////////////// Wasn't sure how to populate the array
theirCourses[0] = new CollegeCourse("CIS 110", 3, 'A');
theirCourses[1] = new CollegeCourse("MATH 330", 2, 'A');
theirCourses[2] = new CollegeCourse("FR ENG 110", 2, 'A');
theirCourses[3] = new CollegeCourse("PHYSICS 110", 1, 'B');
theirCourses[4] = new CollegeCourse("GAMING 110", 1, 'C');
theirCourses[0].setCID("CIS 110");
theirCourses[1].setCID("MATH 330");
theirCourses[2].setCID("FR ENG 110");
theirCourses[3].setCID("PHYSICS 110");
theirCourses[4].setCID("GAMING 110");
theirCourses[0].setHours(3);
theirCourses[1].setHours(2);
theirCourses[2].setHours(2);
theirCourses[3].setHours(1);
theirCourses[4].setHours(1);
theirCourses[0].setGrade('A');
theirCourses[1].setGrade('A');
theirCourses[2].setGrade('A');
theirCourses[3].setGrade('B');
theirCourses[4].setGrade('C');
////////////////////////////////////////////////////////////////// Displays all preset populated courses
System.out.println("Please enter a number for the course you would to view, 0-4.\n"
+ "Courses are:");
for(int x = 0; x < theirCourses.length; ++ x)
{
System.out.println(theirCourses[x].getCID());
}
}
}
如果我沒有在下面的數組中填充重複的語句,我已經將字符串,int和char全部分配在一行中,那麼當我打印語句時,的System.out.println(theirCourses [X] .getCID());我一直得到一個空輸出。任何線索可能是什麼原因呢?我也會發布客戶類。如何正確顯示對象數組中的原始值?
public class CollegeCourse
{
private String cID;
private int cHours;
private char grade;
public CollegeCourse(String string, int i, char c)
{
}
public void setCID(String c)
{
cID = c;
}
public String getCID()
{
return cID;
}
public void setHours(int h)
{
cHours = h;
}
public int getHours()
{
return cHours;
}
public void setGrade(char g)
{
grade = g;
}
public char getGrade()
{
return grade;
}
}
那麼,看看構造函數。它的論點沒有做任何事情(這是非常糟糕的命名,順便說一句)。 –
@captaindex即使有重複任務,你是否得到了'null'輸出? –
在Student.java中添加了兩個右括號後,此代碼在我的工作站上成功運行。 @captaindex,你可以發佈你的輸出嗎? –