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

标签 java class

我想做什么 我是 Java 的完全初学者,昨天才开始学习基础知识。我安装了 Eclipse,我正在尝试制作一个 School Principal 菜单,该菜单可以提供学校所有学生和教师的列表(并添加新的,但那是为了以后)。现在我只想显示现有的学生,因为我还没有创建任何教师。

我有什么 现在我已经创建了我的学生类(class):(如果您发现任何错误或不良形式,请告诉我!)

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) {
    }
}

我也有这个菜单类,首先需要学生人数:

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:");
    }
  }
}

我的问题 现在,输出显然是在菜单开始写入之前打印学生。那是因为我正在 populate void 中打印。问题是,如果我将 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 <---------------------------------------------//

最佳答案

更改填充方法以返回学生的 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;
}

在你的主要部分:

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:");
}

关于java - 从另一个类描述一个类的所有现有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35937657/

相关文章:

java - 本地化和 L&F

java - 调用 SOAP 服务返回 WSDL

c++ - 在 C++ 的另一个 .h 文件中使用类中的方法

jQuery - 显示元素列表后按类别对其进行排序

json - Swift 使用参数初始化类,然后在不使用参数的情况下初始化它

java - 为什么jframe不显示按钮?

java - 将一个点连接到不同点之间的每两个最接近的点

Java线程生产者消费者程序

java - 在多个类中使用一个对象

c++ - 从构造函数传递给成员函数时,私有(private)成员变量为空