我有一個構造函數類作爲這麼叫賦值函數方法:從一個單獨的類
public class Student
{
//declare variables
public int studentID;
public String courseName;
public int courseScore;
public char courseGrade;
//default constructor
public Student()
{
studentID = 1;
courseName = "CIS 2085";
courseScore = 89;
}
//========================================
//constructor with parameters
public Student(int id, String course, int score)
{
setStud(id, course, score);
}
//========================================
public String toString()
{
return (studentID + " " + courseName + " " + courseScore);
}
//========================================
public void setStud(int id, String course, int score)
{
studentID = id;
courseName = course;
courseScore = score;
}
//========================================
public int getStudentID()
{
return studentID;
}
//========================================
public String getCourseName()
{
return courseName;
}
//========================================
public int getCourseScore()
{
return courseScore;
}
//========================================
public char getCourseGrade()
{
return courseGrade;
}
//========================================
public static char calculateGrade(int courseScore)
{
if (courseScore >= 94)
courseGrade = 'A';
else if ((courseScore >= 85) && (courseScore <= 93))
courseGrade = 'B';
else if ((courseScore >= 75) && (courseScore <= 84))
courseGrade = 'C';
else if ((courseScore >= 65) && (courseScore <= 74))
courseGrade = 'D';
else
courseGrade = 'F';
return courseGrade;
}
//========================================
}
,我有我的實現類,到目前爲止,像這樣:
import java.util.*;
public class StudentImplementation
{
//allow 'console' to receive input from the user
static Scanner console = new Scanner(System.in);
//method main
public static void main(String[] args)
{
//declare variables
//object 1: first. Uses the default constructor.
Student first = new Student();
//System.out.println(first);
//object 2: second.
Student second = new Student(3, "Advanced JAVA Programming", 83);
//System.out.println(second);
//use calculateGrade method to receive grade for object 1
char grade = Student.calculateGrade(first.getCourseScore);
System.out.println(grade);
}
}
所以我想使用我的calculateGrade方法根據我的對象courseScore輸出適當的分數。
Student類與我的實現類位於不同的文件中。
當我運行這個程序,我收到錯誤:
Exception in thread "main" java.lang.NoSuchMethodError: Student.calculateGrade(I)C
at StudentImplementation.main(StudentImplementation.java:27)
編輯
你能否解釋一下是什麼問題?我不明白什麼是錯的。 – dguay
我不明白你有什麼問題。你不能簡單地調用calculateGrade方法嗎? –
我想調用方法CalculateGrade在我的實現文件中工作。以便它將採用對象1(第一個)的courseScore並給我courseGrade。 – burgoyne