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

时间:2016-03-11 10:23:59

标签: 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) {
    }
}

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

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空格中正确打印。问题是如果我将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 <---------------------------------------------//

4 个答案:

答案 0 :(得分: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;
}

在你的主要:

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

答案 1 :(得分:4)

您可能会populate返回Student的列表:

   public static List<Student> populate(){

            List<Student> students = new ArrayList<Student>();
            Student s01 = new Student("David");
            s01.age = 12;
            s01.program = "Elementary School";

            students.add(s01);

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

            students.add(s02);

            return students;
        }

然后在main方法中使用此列表:

List<Student> allStudents= Menu.populate();
//.... SNIP
System.out.println("Here is a list of the students:");

for(Student student : allStudents){
 student.PrintInfo();
}

答案 2 :(得分:3)

在我开始回答一些小提示之前:

  • 仅在您的主要课程中需要public static void main(String[] args),因为这是您的申请的入口点
  • Java中的
  • 方法通常写成小写,将PrintInfo更改为printInfo

你想要做的是将学生写到一个其他方法也可以访问它们的地方,因为你有多个学生,我建议也使用ArrayList

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Menu {

    public static List<Student> students = new ArrayList<Student>();

    public static void populate() {
        Student s01 = new Student("David");
        s01.age = 12;
        s01.program = "Elementary School";
        students.add(s01) // Adds student 01 to the list

        Student s02 = new Student("Alex");
        s02.age = 5;
        s02.program = "Kindergarten";
        students.add(s01) // Adds student 02 to the list
    }

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

            for (Student student : students) { // iterate over all the students
                student.printInfo(); // prints the info of every individual student
            }
        } else if (n==2) {
            System.out.println("Here is a list of the teachers:");
        }
    }
}

如果您有任何其他问题,请与我们联系。

答案 3 :(得分:3)

您无法从s01访问main(),因为Student的范围仅限于声明它的括号。在这种情况下,您的学生只能在populate()方法中看到。

如果您想访问它们,您需要:

=&GT;在你的populate方法之外声明它们。例如:

  import java.util.Scanner;  
  public class Menu {

        public static Student s01;
        public static Student S02;

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

            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();
          // call s01 and s02 in you main method
      }
}

=&GT;使populate()方法返回一个包含您创建的学生的列表(在我看来,这是最好的方法)。