"错误:找不到符号"关于继承相关的程序

时间:2015-07-31 19:17:01

标签: java inheritance netbeans-8

我收到error: cannot find symbol关于c1.certificateAwarded(grade);声明的消息。我不知道是什么问题。真的需要我能得到的所有帮助。

以下是代码:

ExamDetails.java

package Exams;

import javax.swing.JOptionPane;

public class ExamDetails {

    public static void main(String[] args) {

        StudentResults sr = new StudentResults();
        sr.inputStudentName();
        sr.inputExamName();
        sr.inputScore();
        sr.inputGrade();
        sr.DisplayDetails();

        Certificates c1 = new Certificates();
        c1.certificateAwarded(grade);
    }

}

StudentResults.java

package Exams;

import javax.swing.JOptionPane;

public class StudentResults {

    private String fullname;
    private String examName;
    private int examScore;
    private int examGrade;

    public String getStudentName()
    {
        return fullname;
    }

    public void setStudentName(String name)
    {
        fullname = name;
    }

    public String getExamName()
    {
        return examName;
    }

    public void setExamName(String exam)
    {
        examName = exam;
    }

    public int getExamScore()
    {
        return examScore;
    }

    public void setExamScore(int score)
    {
        examScore = score;
    }

    public int getExamGrade()
    {
        return examGrade;
    }

    public void setExamGrade(int grade)
    {
        examGrade = grade;
    }

    public void inputStudentName()
    {
        fullname  = JOptionPane.showInputDialog("Enter the student's name");
    }

    public void inputExamName()
    {
        examName  = JOptionPane.showInputDialog("Enter the subject's name");
    }

    public void inputScore()
    {
        String scoreString = new String();
        JOptionPane.showInputDialog("Enter the student's score");
        examScore = Integer.parseInt(scoreString);
    }

    public void inputGrade()
    {
        String gradeString = new String();
        JOptionPane.showInputDialog("Enter the student's grade");
        examGrade = Integer.parseInt(gradeString);
    }

    public String DisplayDetails()
    {
        String d;
        d = "Student Name : " + fullname + "Exam Name : " + examName + "Score : " + examScore + "Grade : " + examGrade;
        return d;
    }
}

Certificates.java

package Exams;

public class Certificates extends StudentResults {

    private String certificate;
    public String Grade;

    Certificates()
    {
        super();
        certificate = "No Certificate Awarded";
    }

    String certificateAwarded(/*int grade*/) {

        //StudentResults g = new StudentResults();
        //Grade = g.inputGrade();
        //Grade = inputGrade(examGrade);

        if(Grade.equals("Grade : A"))
        {
            this.certificate = "Certificate of Excellence";
        }
        else
            if(Grade.equals("Grade : B"))
            {
                this.certificate = "Certificate of Achievement";
            }
            else
                if(Grade.equals("Grade : C"))
                {
                    this.certificate = "Certificate of Achievement";
                }
                else
                    this.certificate = "No Certificate Awardedt";

           return this.certificate;
    }  
}

3 个答案:

答案 0 :(得分:0)

In the ExamDetails.java class, you don't declare or instantiate the grade variable that you're passing to the certificateAwarded method.

Also, you have parameters commented out in your Certificates.java class. You should uncomment the parameter.

答案 1 :(得分:0)

For your code to compile, grade would have to be either

  1. a local variable declared within the main method
  2. a field defined on the ExamDetails class

Neither of these declarations are present so the compiler is telling you that it can't find the grade "symbol".

Try adding int grade = sr.getExamGrade(); above the problem line, or something similar.

答案 2 :(得分:0)

As the comment suggested, you need to have "grade" declared. Without it, the compiler can't complete determining what the signature is for Certficates.certificateAwarded, since what goes in the argument list is part of the signature. More importantly, you have this in your code:

String certificateAwarded(/* int grade */)

The parameter, "int grade" is commented out. So the compiler sees this:

String certificateAwarded( )

So, what the compiler might be telling you is that it is looking for a method of Certificates named certificateAwarded that takes 1 argument of type {whatever type "grade" is}. It doesn't find that.

I said "might be" because you are missing two symbols on the line in question: grade and a method with a matching signature.

I can think of two things you can try to fix it:

  1. Change " c1.certificateAwarded(grade);" to "c1.certificateAwarded();"
  2. Declare "grade" to be an int somewhere in main (or in a place that is visible within main) and change "String certificateAwarded(/int grade/)" to "String certificateAwarded(int grade)".

I would start by trying the first option. If it is necessary to try the second, you will need to add additional code in both main (or place where grade is visible to main) and in the certificateAwarded method.