从另一个类描述类的所有现有元素

Dav*_*d G 4 java class

我想做什么 我是一个完整的Java初学者,昨天开始基础知识.我安装了Eclipse,我正在尝试制作一个学校校长菜单,该菜单可以列出学校里所有学生和教师的名单(并添加新的学生和教师,但这是为了以后).现在我只想展示现有的学生,因为我没有创建任何教师.

我拥有的 目前我已经创建了我的学生课程:(如果您看到任何错误或错误的表格,请告诉我!)

public class Student {

    String name;
    int age;
    String program;

    public Student(String StudentName){
        this.name = StudentName;
    }

    public void PrintInfo(){
        System.out.println(name + " is a " + age +" year old student in " + program);
    }

    public static void main(String[] args) {
    }
}
Run Code Online (Sandbox Code Playgroud)

我也有这个菜单类,首先要求学生群体:

import java.util.Scanner;
public class Menu {

    public static void populate(){
        Student s01 = new Student("David");
        s01.age = 12;
        s01.program = "Elementary School";
        s01.PrintInfo(); //******I would like to remove this part******

        Student s02 = new Student("Alex");
        s02.age = 5;
        s02.program = "Kindergarten";
        s02.PrintInfo(); //******I would like to remove this part******
    }

  public static void main(String[] args) {
      Menu.populate();
    System.out.println("Hello!");
    System.out.println("For a list of students, press 1");
    System.out.println("For a list of teachers, press 2");

    Scanner reader = new Scanner(System.in);  // Reading from System.in
    System.out.println("Enter a number: ");
    int n = reader.nextInt(); // Scans the next token of the input as an int.

    System.out.println("You Entered: " + n);
    if (n==1){
        System.out.println("Here is a list of the students:");

   //******I would like to move the printing here******

    }else if (n==2){
        System.out.println("Here is a list of the teachers:");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的问题 现在,输出显然是在菜单开始写入之前打印的学生.那是因为我正在populate虚空中打印.问题是,如果我切换 s01.PrintInfo();main(),它无法识别s01.如何让程序识别它?

当前和期望的输出

David is a 12 year old student in Elementary School  // I want this //
Alex is a 4 year old student in Kindergarten         //   And this  //
Hello!                                                              //
For a list of students, press 1                                     //
For a list of teachers, press 2                                     //
Enter a number:                                                     //
1                                                                   //
You Entered: 1                                                      //
Here is a list of the students:                                     //
               //here <---------------------------------------------//
Run Code Online (Sandbox Code Playgroud)

ple*_*eft 5

更改您的填充方法以返回List学生

public static List<Student> populate(){
    Student s01 = new Student("David");
    s01.age = 12;
    s01.program = "Elementary School";


    Student s02 = new Student("Alex");
    s02.age = 5;
    s02.program = "Kindergarten";

    List<Student> students = new ArrayList<Student>();
    students.add(s01);
    students.add(s02);
    return students;
}
Run Code Online (Sandbox Code Playgroud)

在你的主要:

if (n==1){
    System.out.println("Here is a list of the students:");

    //******I would like to move the printing here******
    List<Student> students = Menu.populate();
    for(Student student: students) {
        student.PrintInfo();
    }
} else if (n==2){
    System.out.println("Here is a list of the teachers:");
}
Run Code Online (Sandbox Code Playgroud)