在Java中循环新对象并从getMethods传递值?

时间:2016-11-19 18:20:48

标签: java loops

我想知道如何在同一个类中循环一个新对象,这样我就可以重复代码,让几个不同的对象通过Scanner类输入不同的细节。

如果我展示我想要做的事情,可能会有意义:

public class Students
{
    private String studFirstName;       // Student's first name
    private String studSurname;         // Student's surname
    private String courseName;          // Course name

    public Students(String sFirstName, String sSurname, String cName)
    {
        studFirstName = sFirstName;
        studSurname = sSurname;
        courseName = cName;
    }

    public String getStudFirstName()                            // Getter for student's first name
    {
        System.out.println("Enter in student's first name: ");  // Prompts end user for input

        Scanner In = new Scanner(System.in);                    // Creating a new object
        studFirstName = In.next();                              // Accepts a one word input only

        return studFirstName;
    }

    public String setStudFirstName(String sFirstName)           // Setter for student's first name
    {
        return studFirstName;
    }

    public static void main (String [] args)
    {

        Students first[] = new Students[5]; 

        for(Students a : first)
        {
        }

       Students first[] = new Students("", "", ""); 

       String studFirstName = first.getStudFirstName();
    }
}

3 个答案:

答案 0 :(得分:0)

您需要一个常规for循环才能在数组中分配值。

int numStudents = 5;
Student students[] = new Student[numStudents];   // creates an array of 5 nulls

Scanner scan = new Scanner(System.in);
for(int i = 0; i < numStudents; i++) {
    System.out.println("Enter your first name: "); // Prompt
    String fname = scan.nextLine();   // Read
    Student s = new Student(fname);   // Assign
    first[i] = s;                     // Store
}

你真的不需要get方法,因为你有权在constuctor中设置值。

答案 1 :(得分:0)

首先,你应该看看encapsulation。吸气剂并不意味着用户输入。 Getter应该返回私有字段的值。 Setters应该设置私有字段的值。

// This is a field
private String myField;

// This will return the current value for the field 'myField'
public String getMyField() {
    return this.myField;
}

// This will asign a new value to the field 'myField'
public void setMyField(String myField) {
     this.myField = myField;
}

<强>答案

您需要定期循环才能创建所需数量的学生。我重命名了你的班级&#39;学生&#39;学生适合javas命名惯例。

int numberOfStudents = 3;
// create an array that can hold up to 3 students
Student[] students = new Stundent[numberOfStudents];
// create the Scanner to read from console.
Scanner scanner = new Scanner(System.in);
// create the 3 students and store them in the array
for(int i = 0; i < numberOfStudents; i++) {
    System.out.println("Enter name: ");
    // read the next line from the console
    String name = scanner.nextLine();
    // ...
    // if you have anything you need to create the student object (e.g. name, and so on).
    Student s = new Student(name, .....);
    students[i] = s; // store the student object in the array.
}
// you should evt. close the scanner
// scanner.close();
// you can now do anything with your 3 stored students.
System.out.println("The Students first name is: " + students[0].getStudFirstName());

答案 2 :(得分:0)

        int numberOfStudents = 3;
        // create an array that can hold up to 3 students
        Student[] students = new Student[numberOfStudents];
        // create the Scanner to read from console.
        Scanner scanner = new Scanner(System.in);
        // create the 3 students and store them in the array
        for(int i = 0; i < numberOfStudents; i++) {
            System.out.println("Enter name: ");
            // read the next line from the console
            String name = scanner.nextLine();
            // ...
            // if you have anything you need to create the student object (e.g. name, and so on).
            Student s = new Student(studFirstName);
            students[i] = s; // store the student object in the array.
        }


    // you should evt. close the scanner
    // scanner.close();
    // you can now do anything with your 3 stored students.
    System.out.println("The Students first name is: " + students[0].getStudFirstName());
}

}