Java 从另一个类描述一个类的所有现有元素
我想做什么 我是一个Java的完全初学者,从昨天的基础知识开始。我安装了Eclipse,我正在尝试制作一个学校校长菜单,可以列出学校里所有的学生和老师(也可以添加新的学生和老师,但这是为以后准备的)。现在我只想展示现有的学生,因为我还没有创建任何教师 我拥有的 现在我已经创建了我的学生班:(如果你看到任何错误或不好的形式,请告诉我!) 我还有一门菜单课,它首先要求学生人数:Java 从另一个类描述一个类的所有现有元素,java,class,Java,Class,我想做什么 我是一个Java的完全初学者,从昨天的基础知识开始。我安装了Eclipse,我正在尝试制作一个学校校长菜单,可以列出学校里所有的学生和老师(也可以添加新的学生和老师,但这是为以后准备的)。现在我只想展示现有的学生,因为我还没有创建任何教师 我拥有的 现在我已经创建了我的学生班:(如果你看到任何错误或不好的形式,请告诉我!) 我还有一门菜单课,它首先要求学生人数: import java.util.Scanner; public class Menu { public sta
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 <---------------------------------------------//
David是一名12岁的小学生//我想要这个//
Alex是一名4岁的幼儿园学生//这个//
你好!//
要查看学生列表,请按1//
要查看教师列表,请按2//
输入一个数字://
1 //
您输入:1//
以下是学生名单://
//在这里您可能需要填充返回学生的列表
:
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;
}
更改填充方法以返回学生的列表
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;
}
公共静态列表填充(){
学生s01=新学生(“大卫”);
s01.年龄=12岁;
s01.program=“小学”;
学生s02=新学生(“亚历克斯”);
s02.年龄=5岁;
s02.program=“幼儿园”;
List students=new ArrayList();
学生。添加(s01);
学生。添加(s02);
留学生;
}
主要是:
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:");
}
if(n==1){
System.out.println(“这是学生名单:”);
//******我想把印刷移到这里******
List students=Menu.populate();
用于(学生:学生){
student.PrintInfo();
}
}else如果(n==2){
System.out.println(“这是一个教师列表:”);
}
在我开始回答一些小提示之前:
publicstaticvoidmain(String[]args)
仅在主类中需要,因为它是应用程序的入口点
- Java中的方法通常以小写形式编写,将
PrintInfo
更改为PrintInfo
你想做的是把学生写到一个其他方法也可以访问他们的地方,因为你有多个学生,我建议你也使用一个
import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公共类菜单{
public static List students=new ArrayList();
公共静态void populate(){
学生s01=新学生(“大卫”);
s01.年龄=12岁;
s01.program=“小学”;
add(s01)//将student 01添加到列表中
学生s02=新学生(“亚历克斯”);
s02.年龄=5岁;
s02.program=“幼儿园”;
add(s01)//将student 02添加到列表中
}
公共静态void main(字符串[]args){
Menu.populate();
System.out.println(“你好!”);
System.out.println(“要查看学生列表,请按1”);
System.out.println(“要查看教师列表,请按2”);
扫描仪阅读器=新扫描仪(System.in);//从System.in读取
System.out.println(“输入一个数字:”);
int n=reader.nextInt();//将输入的下一个标记扫描为int。
System.out.println(“您输入:“+n”);
如果(n==1){
System.out.println(“这是学生名单:”);
for(Student:students){//迭代所有学生
student.printInfo();//打印每个学生的信息
}
}else如果(n==2){
System.out.println(“这是一个教师列表:”);
}
}
}
如果您有任何其他问题,请告诉我。您无法从main()
访问s01
,因为学生的范围仅限于声明的括号内。在这种情况下,您的学生仅在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
}
}
=>让populate()
方法返回一个包含您创建的学生的列表(我认为这是最好的方法) 非常感谢!我知道解决办法很简单!谢谢我只能接受一个答案,但有权投赞成票;)其他人提到了列表选项,这就是我将要做的。但是在外面申报呢?是否只是宣布一名公立学生()?请编辑您的答案,告诉我如何:Dsee更新答案:现在,范围是整个菜单类。列表选项是一个很好的选项,我会选择它。我只是想确保您注意java中变量作用域的概念。谢谢您的回答。声明ArrayList时,我得到“ArrayList无法解析为类型”。你知道为什么吗?我想你错过了导入,我已经在示例中添加了导入。在Eclipse中,您还可以使用Shift+Ctrl+O进行自动导入。我不知道有些型号需要进口。谢谢!
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:");
}
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:");
}
}
}
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
}
}