java - 类型数组的方法未定义
问题描述
**大家好。我是这个平台的新手,我需要一些关于 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);
}
}
}
解决方案
解决该问题的一种方法是使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);
}
}
推荐阅读
- flutter - 在 Flutter 中创建复选框
- dns - 无法使用 OpenVPN 解析对等 VPC 的 DNS
- python - 使用 xs 对多索引 pandas 数据框进行切片
- python - Abaqus 脚本中的节点应力和应变| 一个节点有两种不同的应力值?
- java - 停用骆驼 cxf:cxfEndpoint
- c# - 如何在c#中使用表格的html隐藏带有条件的特定li项目
- html - 使用时具有单行行为
- flutter - FCM 的问题,不知道在 AndroidManifest 中放什么
- outlook - 通过 Microsoft Graph API 删除 Outlook 类别不会将其从邮件中删除
- python - 基于 numpy 提高脚本的时间和内存