StudentDemo:調用的方法在另一類來創建一個數組
public class studentDemo {
public static void main(String[] args) throws IOException{
int runit = 0;
StudentMenu run = new StudentMenu();
while (runit != 1) {
run.menu();
}
}
}
StudentMenu:
public class StudentMenu {
public Student func = new Student();
List<Student> studentList = new ArrayList<Student>();
String input;
String input2;
String input3;
public void menu(){
System.out.println("Choose an option: ");
System.out.println("1) Add a student ");
System.out.println("2) Display student list ");
System.out.println("3) Exit ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
input = br.readLine();
int choice = Integer.parseInt(input);
System.out.println("Choice: " + choice);
if(choice == 1){
System.out.println("First name: ");
input = br.readLine();
System.out.println("Last name: ");
input2 = br.readLine();
System.out.println("Number of classes to add for student: ");
input3 = br.readLine();
int numberOfClasses = Integer.parseInt(input3);
studentList.add(new Student(input, input2, numberOfClasses));
func.createClassList(numberOfClasses);
}
if(choice == 2){
}
if(choice == 3){
System.exit(0);
}
}
catch (IOException ioe){
System.out.println("An unexpected error occured.");
}
}
}
學生:
public class Student{
private String firstName;
private String lastName;
private int classes;
//Create array for classes
private String[] classesList = new String[classes];
public Student(){
firstName = "no name";
lastName = "no name";
classes = 0;
}
//overloaded constructor
public Student(String theFirstName, String theLastName, int theClasses){
firstName = theFirstName;
lastName = theLastName;
classes = theClasses;
System.out.println("Number of Classes: " + classes);
}
//Populating array
public void createClassList(int classes){
//Call array method
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
for(int i = 0; i < classes; i++){
System.out.println("Number of Classes: " + classes);
System.out.println("Enter class " + i + ":");
String input = br.readLine();
classesList[i] = input;
}
}
catch (IOException ioe) {
System.out.println("An unexpected error occured.");
}
}
//Getters
public String getFirstName(){ return firstName; }
public String getLasName(){ return lastName; }
public int getNumberOfClasses(){ return classes; }
//Setters
public void setFirstName(String newFirstName){
firstName = newFirstName;
}
public void setLastName(String newLastName){
firstName = newLastName;
}
public void setNumberOfClasses(int newNumberOfClasses){
classes = newNumberOfClasses;
}
}
很抱歉,如果我過去有點不正確地編輯了這一點。
我想創建一個程序,用他們的名字,姓氏和類別列表建立一個學生列表。不過,我一直運行到這個錯誤輸入第一個下課:
,這裏是錯誤之前的輸出:
右後Choose an option:
1) Add a student
2) Display student list
3) Exit
1
Choice: 1
First name:
t
Last name:
s
Number of classes to add for student:
3
Enter class 0:
eng
打字ENG,錯誤出現。
不應該有我可以告訴的OutOfBoundsException,因爲3是索引的限制,我只能在一個類中輸入。
任何幫助將不勝感激,謝謝!
你能否嘗試將代碼粘貼是否正確?它會幫助我們幫助你。 – 2014-12-05 01:43:35