求助:我首次编写的Java printDegreeClassification方法代码是否正确?
Is my first Java program for printing degree classification correct?
Hey there! Congrats on writing your first Java program—let's walk through what's going wrong and how to fix it. Your logic for determining the degree classification is actually on the right track, but there are a few syntax and structure issues that will prevent your code from compiling.
Key Issues in Your Code
- Methods can't exist outside a class: Your
printDegreeClassificationmethod is floating on its own, not inside any class. Java requires all methods and classes to be defined within a top-level class (or nested classes, but that's more advanced). - Unimplemented method in Student class: The
getAverage()method in yourStudentclass only has a declaration (public int getAverage();) but no actual code to return a value. The compiler will throw an error here because it doesn't know what value to return. - Invalid nesting: You've defined the
Studentclass andmainmethod inside theprintDegreeClassificationmethod—this isn't allowed in Java. Top-level classes and themainentry point need to be at the root level of your code file. - Incorrect method invocation: Your
mainmethod tries to callresult.printDegreeClassification(result), butprintDegreeClassificationisn't a method of theStudentclass in your original code. You need to call it from the class that contains it (or adjust your design to make it part of theStudentclass).
Fixed Code Option 1: Separate Class for Classification Logic
This keeps the Student class focused on holding student data, and uses a separate class to handle the degree classification:
// Student class to hold student data public class Student { private int average; // Constructor to set the student's average score public Student(int average) { this.average = average; } // Fully implemented getAverage method public int getAverage() { return average; } } // Class to handle degree classification public class DegreeClassifier { public void printDegreeClassification(Student student) { int average = student.getAverage(); if (average >= 70) { System.out.println("First"); } else if (average >= 60) { System.out.println("Upper Second"); } else if (average >= 50) { System.out.println("Lower Second"); } else if (average >= 40) { System.out.println("Third"); } else if (average >= 30) { System.out.println("Pass"); } else { System.out.println("Fail"); } } public static void main(String[] args) { // Create a student with an example average (65 in this case) Student student = new Student(65); // Create a classifier instance and call the method DegreeClassifier classifier = new DegreeClassifier(); classifier.printDegreeClassification(student); } }
Fixed Code Option 2: Classification as Part of Student Class
If you prefer to keep the classification logic directly in the Student class (which makes sense if it's student-related behavior):
public class Student { private int average; public Student(int average) { this.average = average; } public int getAverage() { return average; } // Move the classification method into the Student class public void printDegreeClassification() { int average = getAverage(); if (average >= 70) { System.out.println("First"); } else if (average >= 60) { System.out.println("Upper Second"); } else if (average >= 50) { System.out.println("Lower Second"); } else if (average >= 40) { System.out.println("Third"); } else if (average >= 30) { System.out.println("Pass"); } else { System.out.println("Fail"); } } public static void main(String[] args) { // Create a student and call the method directly Student student = new Student(65); student.printDegreeClassification(); } }
Quick Notes
- Make sure each top-level class is in a file with the same name (e.g.,
Student.javafor theStudentclass,DegreeClassifier.javafor the first option). - The constructor in the
Studentclass lets you set the average score when creating a student instance—you can adjust this to fit how you plan to input scores later.
内容的提问来源于stack exchange,提问作者Alina Khachatrian
相关产品推荐
相关产品推荐

