You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:我首次编写的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 printDegreeClassification method 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 your Student class 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 Student class and main method inside the printDegreeClassification method—this isn't allowed in Java. Top-level classes and the main entry point need to be at the root level of your code file.
  • Incorrect method invocation: Your main method tries to call result.printDegreeClassification(result), but printDegreeClassification isn't a method of the Student class in your original code. You need to call it from the class that contains it (or adjust your design to make it part of the Student class).

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.java for the Student class, DegreeClassifier.java for the first option).
  • The constructor in the Student class 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:52:31