首页 > 解决方案 > 类型数组的方法未定义

问题描述

**大家好。我是这个平台的新手,我需要一些关于 JAVA 代码的帮助。

代码中有这个错误,我不知道如何解决。谁能帮我这个?**

        import java.util.*;

        public class Q3 {
        public static void main(String args[])
        {
        Scanner sc=new Scanner(System.in);
        Exam e[]= new Exam[5];

        for(int i=1;i<=5; i++)
        {
            e[i]=new Exam();
        }

        for(int i=1;i<=5;i++)
        {
        System.out.println("Enter the details of the student: His name, course and roll no. 
        respectively:");
        String name=sc.nextLine();
        String course=sc.nextLine();
        int roll=sc.nextInt();

        System.out.println("Enter the mark1, mark2 and mark3 respectively:");
        int mark1=sc.nextInt();
        int mark2=sc.nextInt();
        int mark3=sc.nextInt();

        e[i].input_Student(roll, name,course);
        e[i].input_Marks(mark1, mark2, mark3);
        }
        System.out.println("The result is displayed below:");
        for(int i=1; i<=5;i++)
        {
            e[i].display_Student();


e[i].display_Result();

**这是我面临的问题。它说 - 方法 display_Marks() 未定义为 Exam 类型 - 方法 display_Result() 未定义为 Exam**

        }
        }
        }
         class Student
        {
        int roll;
        String name;
        String course;

        public void input_Student(int roll, String name, String course)
        {
        this.roll=roll;
        this.name=name;
        this.course=course;
        }
        void display_Student()
        {
        System.out.println("Roll no:"+roll+", Name:"+name+", Course"+course);

        }
         class Exam extends Student
        {
        int mark1, mark2,mark3;
         void input_Marks(int mark1, int mark2, int mark3)
        {
            this.mark1=mark1;
            this.mark2=mark2;
            this.mark3=mark3;
        }
         void display_Result()
        {
            System.out.println("mark1:"+mark1+", mark2:"+mark2+", mark3:"+mark3);
        }
        }
        }

标签: java

解决方案


解决该问题的一种方法是使Exam类成为静态的。

但是建议做Exam一个单独的类而不是嵌套在Student类中

public class Q3 {

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Exam e[] = new Exam[5];

        for (int i = 1; i <= 5; i++) {
            e[i] = new Exam();
        }

        for (int i = 1; i <= 5; i++) {
            System.out.println("Enter the details of the student: His name, course and roll no. respectively:");
            String name = sc.nextLine();
            String course = sc.nextLine();
            int roll = sc.nextInt();

            System.out.println("Enter the mark1, mark2 and mark3 respectively:");
            int mark1 = sc.nextInt();
            int mark2 = sc.nextInt();
            int mark3 = sc.nextInt();

            e[i].input_Student(roll, name, course);
            e[i].input_Marks(mark1, mark2, mark3);
            sc.nextLine();
        }
        System.out.println("The result is displayed below:");
        for (int i = 1; i <= 5; i++) {
            e[i].display_Student();
            e[i].display_Result();
        }
    }
}

class Student {

    int roll;
    String name;
    String course;

    public void input_Student(int roll, String name, String course) {
        this.roll = roll;
        this.name = name;
        this.course = course;
    }

    void display_Student() {
        System.out.println("Roll no:" + roll + ", Name:" + name + ", Course" + course);

    }
}

class Exam extends Student {

    int mark1, mark2, mark3;

    void input_Marks(int mark1, int mark2, int mark3) {
        this.mark1 = mark1;
        this.mark2 = mark2;
        this.mark3 = mark3;
    }

    void display_Result() {
        System.out.println("mark1:" + mark1 + ", mark2:" + mark2 + ", mark3:" + mark3);
    }
}

推荐阅读