Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建自定义类的数组_Java - Fatal编程技术网

Java 如何创建自定义类的数组

Java 如何创建自定义类的数组,java,Java,我对Java和一般的编码有点陌生,我遇到了一个到目前为止我无法解决的问题。 功能是:一个开关菜单,要求将输入保存在数组选项1上,然后使用第二个选项打印数组中对象的属性 我有一个自定义类: 课程 public class Course { String name_course; String code_course; int credits_course; Course(String name, String code, int credits){

我对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]);

        }


    }
}

如果能帮我找出错误,我将不胜感激。
谢谢。

欢迎使用stack overflow,我认为您的代码比需要的代码复杂了一点—实际上并不需要类逻辑。但从课程开始,这里是我的版本

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而不是构造函数方法。请看一下。我还使用@Override注释来重写的标准toString方法。 现在,对于控件的实现,逻辑类的主要问题是,您仅限于6门课程,如果您需要更多的课程,该怎么办 幸运的是,Java拥有一个可以处理任何类的动态大小的数组 下面是我的示例代码

  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());
    }

}

我想你已经明白了这个想法,希望这对Java有用。如果你刚刚开始,你可以使用列表,更具体的数组列表。他们会让你的生活轻松很多。我相信你没有代码问题,但逻辑问题,虽然有些人可能会认为他们是一样的事情。退一步,重新思考你必须做的事情,用文字写出来,甚至是一步一步的过程,然后将该大纲转换为代码。print方法参数的可能重复项称为pcourse,但最终打印的是静态的one course参数-我认为您需要将该参数重命名为course或打印作为参数传递的数组-名为pcourse,而不是静态定义的-即,将其更改为:out.printlnpcourse[i];
  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());
    }

}
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