我有一个构造函数类:
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)
已编辑
答案 0 :(得分:3)
将calculateGrade方法更改为:
public char calculateGrade() {
courseScore = this.getCourseScore(); // this refers to the student from whom you called the calculaGrade() method --> first.calculateGrade()
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;
}
然后只使用first.calculateGrade() 并打印结果。
Student first = new Student();
char firstStudentGrade = first.calculateGrade();
System.out.println(firstStudentGrade);
PS:方法名称不以大写字母开头;)
答案 1 :(得分:3)
这是您的Student课程的更干净版本,可以完成您希望它执行的操作。我已经取出了不必要的变量和方法,并将其清理了一下。
我这样做的原因是因为现在,你的代码包含很多没有意义的东西(比如在方法中添加一个参数,这样你就可以在方法本身设置参数值然后返回它,在构造函数中使用setter方法。)
public class Student {
public int studentID;
public String courseName;
public int courseScore;
public Student() {
this(1, "CIS 2085", 89);
}
public Student(int id, String course, int score) {
this.studentID = id;
this.courseName = course;
this.courseScore = score;
}
@Override
public String toString() {
return studentID + " " + courseName + " " + courseScore;
}
public int getStudentID() {
return studentID;
}
public String getCourseName() {
return courseName;
}
public int getCourseScore() {
return courseScore;
}
public char getCourseGrade() {
if (courseScore >= 94) {
return 'A';
} else if (courseScore >= 85 && courseScore <= 93) {
return 'B';
} else if (courseScore >= 75 && courseScore <= 84) {
return 'C';
} else if (courseScore >= 65 && courseScore <= 74) {
return 'D';
}
return 'E';
}
}
以下是一个示例实现:
public class StudentImplementation {
public static void main(String[] args) {
Student first = new Student(); //default constructor
System.out.println(first.getCourseGrade());
Student second = new Student(3, "Advanced JAVA Programming", 83);
System.out.println(second.getCourseGrade());
}
}
答案 2 :(得分:0)
怎么样
public char CalculateGrade()
{
if (this.courseScore >= 94)
courseGrade = 'A';
else if ((this.courseScore >= 85) && (this.courseScore <= 93))
courseGrade = 'B';
else if ((this.courseScore >= 75) && (this.courseScore <= 84))
courseGrade = 'C';
else if ((this.courseScore >= 65) && (this.courseScore <= 74))
courseGrade = 'D';
else
courseGrade = 'F';
g = courseGrade;
return g;
}
并将其命名为
System.out.println(first.CalculateGrade());
答案 3 :(得分:0)
创建Student
对象后,只需调用您的方法......
Student first = new Student();
first.CalculateGrade();
并从您不需要的g
中移除CalculateGrade(char gg)
参数。
答案 4 :(得分:0)
更改语句
char grade = Student.calculateGrade(first.getCourseScore);
到
char grade = Student.calculateGrade(first.getCourseScore());
在方法调用期间您错过了()
。