未定义类型数组的方法

时间:2020-06-06 08:35:07

标签: java

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

代码中存在此错误,我不知道如何解决。 有人可以帮我吗?**

do { n = ...; } while (n < 1 || n > 8);

**这是我面临的问题。它说-方法display_Marks()对于 考试类型 -未定义方法display_Result() 输入考试**

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

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是使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);
    }
}