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

我想做的事

我是

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值