我想做的事
我是
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:");
}
}
}
我的问题
现在,输出显然是在菜单开始写入之前打印的学生.那是因为我在填充空白中正确打印.问题是,如果我切换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