java简单的对象数组

时间:2014-05-07 04:30:28

标签: java arrays oop

我是Java的新手,甚至是OOP的新手。我正在尝试为我的一个班级完成这项任务。

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

为学生创建课程。该类必须包含每个学生的姓名,(String),ID(int)和status(int。)

状态表明学生的上课时间:1名为大一,2名为大二,3名为大三,4名为大四。

创建名称为Name1,Name2等的20名学生到Name20,并随机分配谁的ID和状态。打印出一大堆学生 对象。

查找所有青少年并打印他们的姓名和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;
    }
}

方法randInt是我尝试使用math.random(),但我不知道如何让它显示在1到4之间,就像要求一样。

我不妨问我在这里,有没有人有任何想法扫描阵列找到青少年?

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

你也可以查看我的代码并简单地指出我的愚蠢错误吗?

非常感谢您提供任何帮助!

3 个答案:

答案 0 :(得分:1)

你搞砸了一些事情:

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()的一部分(也是静态的)。这些方法应该有一个视图&#34;学生班的所有实例。

  • 在此处查看如何使用Math.random()Math.random() explained

这些是基本错误,我甚至没有检查你的方法的逻辑。从那开始并添加一些日志打印(或运行&#34; debug&#34;模式)以查看逻辑错误

答案 1 :(得分:0)

我想知道你的代码是否编译? 有一个stuName = Name + i;我没有看到Name在任何地方声明。 打印小辈,

在伪代码中,

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

我不会将我的学生数组命名为'name'。这很令人困惑。

答案 2 :(得分:0)

我不经常这样做,但你已经在这种情况下表现出足够的努力,我认为这是有道理的。除了alfasin的回答中提到的要点之外,这里有一个正确的版本,内联注释和简化。

请注意,public类为class Classroom,因此除非您重命名该类,否则此代码需要存在于名为Classroom.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);
        }
    }
}