gpt4 book ai didi

java - 该方法对于类型数组未定义

转载 作者:行者123 更新时间:2023-12-01 16:22:42 24 4
gpt4 key购买 nike

**大家好。我是这个平台的新手,我需要一些有关 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() 未定义 类型 考试 - 方法 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);
}
}
}

最佳答案

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

关于java - 该方法对于类型数组未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62229276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com