首页 > 解决方案 > 如何创建自定义类的数组

问题描述

我对 Java 和一般编码有点陌生,我遇到了一个到目前为止我无法解决的问题。功能是: 一个开关菜单,要求将输入保存在数组中(选项 1),然后使用第二个选项打印数组中对象的属性。

我有一个自定义类:

课程

 public class Course {


    String name_course;
    String code_course;
    int credits_course;

    Course(String name, String code, int credits){

        this.name_course = name;
        this.code_course = code;
        this.credits_course = credits;

    }
}

在另一个文件中,我定义了一个函数,用于将用户的输入保存在数组中,还定义了循环遍历数组并打印值的函数。

public class Logic{


    static BufferedReader in = new BufferedReader(
            new InputStreamReader(System.in));
    static PrintStream out = System.out;


    //I believe this function does not save correctly the input on the array 
    static Course[] course = new Course[6];

    public static void register_course(String name, String code, int credits) {

        for (int i = 0; i < course.length; i++) {
            course[i] = new Course(name, code, credits);



        }

   // This only prints one value as the previous function is likely wrong 
   public static void print_course(Course[] pcourse) {


        for (int i = 0; i < course.length; i++) {
            out.println(course[i]);

        }


    }
}

这是我在主上的开关


 // Just to clarify I have a do while loop that loops over the switch but I won't include it, it works fine 
 public static void process_option(int pOption) throws IOException{
        switch(pOption){

            case 1:
                out.println("Name");
                String name = in.readLine();
                out.println("Code");
                String code = in.readLine();
                out.println("Credits");
                int credits = Integer.parseInt(in.readLine());
                Logic.register_course(name, code, credits);


                break;

            case 2:
                Logic.print_course(Logic.course);

                break;

    }


我非常感谢任何帮助找出我的错误。谢谢。

标签: java

解决方案


欢迎来到堆栈溢出,我认为您的代码比实际需要的要复杂一些,Logic实际上并不需要该类。但是少从班开始Course这里是我的版本

public class Course {
    private String nameCourse;
    private String codeCourse;
    private int creditsCourse = 0;


   public void nameCourse(String name){
     this.nameCourse = name;
   }

   public void codeCourse(String name){
     this.codeCourse = name.toUpperCase();
   }


   public void creditsCourse(int credits){
     this.creditsCourse = credits;
   }


   @Override
   public String toString(){
       return "Course " + this.nameCourse + " with code " +  this.codeCourse + " credit for the course " + this.creditsCourse;
   }

}

它与您的工作完全相同,但它使用 setter 而不是构造函数方法,请查看此Setter。我还使用@Override注解覆盖了Object 类的标准 toString() 方法。现在对于您的控件实现,您的课程的主要问题Logic是您仅限于 6 门课程如果您需要更多课程对我们来说幸运的是,Java 有ArrayList可以处理任何类的动态大小的数组这是我的示例代码

  public static void main(String[] args) {
    ArrayList<Course> courses = new ArrayList<>();
    Course course;
    for (int counter = 0; counter < 10; counter++) {
        course = new Course();
        course.codeCourse("cs" + counter);
        course.nameCourse("Computer Science " + counter);
        course.creditsCourse(10 + counter);
        courses.add(course);
    }

    for (Course c : courses) {
        System.out.println(c.toString());
    }

}

您可以看到声明ArrayList<Course> courses = new ArrayList<>()并添加到新课程的列表中courses.add(course),最后System.out.println(c.toString())将对象的内容打印为字符串。

输出看起来像

Course Computer Science 0 with code CS0 credit for the course 10
Course Computer Science 1 with code CS1 credit for the course 11
Course Computer Science 2 with code CS2 credit for the course 12
Course Computer Science 3 with code CS3 credit for the course 13
Course Computer Science 4 with code CS4 credit for the course 14
Course Computer Science 5 with code CS5 credit for the course 15
Course Computer Science 6 with code CS6 credit for the course 16
Course Computer Science 7 with code CS7 credit for the course 17
Course Computer Science 8 with code CS8 credit for the course 18
Course Computer Science 9 with code CS9 credit for the course 19

我想你明白了,希望这对 Java 有用


推荐阅读