java简单对象数组

java简单对象数组,java,arrays,oop,Java,Arrays,Oop,我对Java是新手,对OOP更是新手。我正在为我的一门课完成这项作业 B.对象数组编写满足以下要求的程序: 为学生创建一个班级。该类必须包含每个学生的姓名(字符串)、ID(int)和状态(int.) 该状态表示该学生的班级排名:一年级1名,二年级2名,三年级3名,四年级4名 创建20名学生,其姓名为Name1、Name2等,并将其ID和状态随机分配给Name20。打印出学生的数组 对象 查找所有JUnitor并打印其名称和ID 注意:使用Math.random()方法创建学生ID和状态 我知道这

我对Java是新手,对OOP更是新手。我正在为我的一门课完成这项作业

B.对象数组编写满足以下要求的程序:

为学生创建一个班级。该类必须包含每个学生的姓名(字符串)、ID(int)和状态(int.)

该状态表示该学生的班级排名:一年级1名,二年级2名,三年级3名,四年级4名

创建20名学生,其姓名为Name1、Name2等,并将其ID和状态随机分配给Name20。打印出学生的数组 对象

查找所有JUnitor并打印其名称和ID

注意:使用Math.random()方法创建学生ID和状态

我知道这是相对简单的,但我只是不太明白。以下是我目前的代码:

class ArryOfObjects{
    public static void main(String[] args){
        String stuName;
        int stuID, stuStatus;
        Student [] name = new Student[20];
        int i;
        while(i < name.length){
            name[i] = new Student(creatStuInfo()); //hopefully this loads objects into arrays
            i++;
        }
    }
}


class Student{
    String stuName;
    int stuID, stuStatus;
    Student(){
        stuName = this.stuName;
        stuID = this.stuID;
        stuStatus = this.stuStatus;
    }
    public void creatStuInfo(int i){
        int min = 1;
        int max = 4;
        String stuName;
        int stuID, stuStatus;
        stuID = Math.random();
        stuStatus = randInt();
        stuName = Name + i;
    }
    public int randInt(int min, int max){
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }
}
类ArryOfObjects{
公共静态void main(字符串[]args){
弦乐特技;
int stuID,stuID状态;
学生[]姓名=新学生[20];
int i;
while(i
randInt方法是我尝试使用math.random()的方法,但我不确定如何让它像要求的那样显示在1到4之间

我在这里的时候不妨问一下,有没有人对扫描阵列以找到年轻人有什么想法

在这种特殊情况下,你会如何打印出这些对象

你也能看看我的代码,简单地指出我愚蠢的错误吗


非常感谢你能给予的任何帮助

你把一些事情搞砸了:

Student(){
    stuName = this.stuName;
    stuID = this.stuID;
    stuStatus = this.stuStatus;
}
你可能想做:

Student(String stuName, int stuID, int stuStatus){
    this.stuName = stuName;
    this.stuID = stuID;
    this.stuStatus = stuStatus;
}
  • 其次,生成学生的所有方法都应该是静态的,或者是
    main()
    (也是静态的)的一部分。这些方法应该具有类Student的所有实例的“视图”

  • 请参见如何使用
    Math.random()
    此处:


这些是基本的错误,我甚至没有检查你的方法的逻辑。从这开始,添加一些日志打印(或在“调试”模式下运行)以查看逻辑错误

我想知道您的代码是否可以编译? 有一个stuName=Name+i;我在任何地方都看不到这个名字。 要打印青少年

在伪代码中

for (int i=0;i<20;i++){//since you declared the array as 20
   if (array[i].stuStatus==3){
      System.out.println("i'm a junior.");
   }
}

for(int i=0;i我不经常这样做,但在这种情况下,您已经付出了足够的努力,我认为这是有道理的。除了alfasin回答中提到的要点外,这里还有一个正确的版本,带有内联注释和简化

请注意,
public
类是
class-school
,因此除非重命名该类,否则该代码需要保存在一个名为
school.java
的文件中

import java.util.*;

class Student{
    String name;
    int id;
    int status;

    // Tool for generating random numbers
    static Random random = new Random();

    // Constructor
    public Student(String name){
        this.name = name;
        status = random.nextInt() % 4 + 1; // Get a number from 0 to 3 using mod, and then add 1 to make it 1 to 4
        id = random.nextInt();
    }
}

public class Classroom{
    public static void main(String[] args){
        // A better name would be "students" but I kept your variable name.
        Student[] name = new Student[20];

        // The standard way to go through an array is a for loop, not a while loop.
        for (int i = 0; i < name.length; i++) {
            String currentName = "Name" + (i+i);
            name[i] = new Student(currentName); //hopefully this loads objects into arrays
        }

        // Find all the juniors. Note that this could have been done in the earlier loop.
        for (int i = 0; i < name.length; i++) {
            if (name[i].status == 3)
                System.out.println(name[i].name + " " + name[i].id);
        }
    }
}
import java.util.*;
班级学生{
字符串名;
int-id;
智力状态;
//生成随机数的工具
静态随机=新随机();
//建造师
公立学生(字符串名称){
this.name=名称;
status=random.nextInt()%4+1;//使用mod获取一个从0到3的数字,然后添加1使其成为1到4
id=random.nextInt();
}
}
公共课堂{
公共静态void main(字符串[]args){
//更好的名字应该是“学生”,但我保留了你的变量名。
学生[]姓名=新学生[20];
//遍历数组的标准方法是for循环,而不是while循环。
for(int i=0;i