1

I am very new to java and I am not sure what i need to use in order to accomplish my goal... or I am not sure how to search for it on google or on stackoverflow...

Suppose I have a class Student that has a method to get/set their name, and their major. I also have another class Major which I can create an instance and add students that belong to this major in an ArrayList.

I have a Scanner so that the student can input their details...

I would write it like this in my Student class with a main:

    package exp;
import java.util.Scanner;
public class Student {

    private String name, major;
    private int id;

    public Student(){
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setMajor(String major){
        this.major = major;
    }
    public String getMajor(){
        return major;
    }    
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Student student1 = new Student();
        System.out.println("Enter name: ");
        student1.setName(scan.next());
        System.out.println("Enter major: ");
        student1.setMajor(scan.next());


        Major major1 = new Major();
        major1.addStudents(student1);
        System.out.println("List of students in major1: ");
        major1.getStudents(); 
    }
}

in my Major class:

package exp;
import java.util.ArrayList;
public class Major {

private ArrayList<Student> students;

public Major(){
    students = new ArrayList<Student>();
}

public void addStudents(Student insertStudent){
    students.add(insertStudent);
}
public void getStudents(){
    for(Student student: students){
        System.out.println(student.getName());
    }
}

}

I understand this works fine... the student fills in their details... but what if there are 1000 or unknown number of students?? how do we get to create new instances instead of manually creating in the main "Student student2 = new Student();" and so on...and also how do we get to add the instance of a Student into the instance of a Major that they belong to?

Thank you very much!

4

4 に答える 4

3

You can simply put the majority of your main method in a while(true) loop, which will loop forever until the user inputs some sentinel value ("Enter 0 to quit"). When they enter "0," break out of the loop:

if(scan.nextLine().equals("0")){ break; }

Hope that helps!

于 2012-11-18T02:01:57.090 に答える
1

whileループを使用するだけです

Scanner scan = new Scanner(System.in);
while(true)
{
        Student student = new Student();
        System.out.println("Enter name: ");
        student.setName(scan.next());
        System.out.println("Enter major: ");
        student.setMajor(scan.next());
        System.out.println("Do you wish to continue [Y/N] ?");
        if(scan.next().equalsIgnoreCase("N"))
        {
            break;
        }   
}
于 2012-11-18T02:03:37.737 に答える
0

student1が に追加されると、その参照を削除して、新しいオブジェクト値ArrayListを与えることができます。Student

于 2012-11-18T02:03:47.007 に答える
0

まず、Major を設定するときは、代わりに major1 変数を使用し、クラス Student の major 属性を「Major」にします。

少佐の宣言から始める

ループを使用してすべてを埋めます

Major major1 = new Major();

while(true) {
    Student temp = new Student();
    System.out.println("please enter your name...");
    temp.setName(scan.next());
    System.out.println("Please enter the name of your major.");
    Major majTemp = null;
    while(majTemp == null) majTemp = getMajorPerName(scan.next());
    temp.setMajor(majTemp);
    majTemp.addStudent(temp.getName());
}
于 2012-11-18T02:09:33.073 に答える