Java-学生类编译错误

时间:2019-03-18 21:00:59

标签: java

我已经编写了此Java代码,但遇到错误,有人可以告诉我为什么吗?该代码将创建一个学生班级,并将通过测试。 我是Java新手,所以将不胜感激 这是代码:

import java.io.*;
import java.util.*;

public class Student {
private static void main(String[] args) 
    {

                private String forName;
                private String surName;
                private String studentID;
                private String degreeScheme;

            //This is the Constructor of the 
class Student
                public Student(String name) {
                    this.forName = forName;
                }
            //Assign the surname of the student 
                public void stuSurname (String 
stuSurname){
                    surName = stuSurname;
                }
            //Assign the student ID to the 
student
                public void stuID (String stuID){
                    studentID = stuID;
                }
            //Assign the Degree of the Student
                public void stuDegree (String 
stuDegree){
                    degreeScheme = stuDegree;

                }

            //Print the student details
                public void printStudent(){

System.out.println("Forname:"+ forName);

System.out.println("Surename:"+ surName);
                    System.out.println("Student 
ID:"+ studentID);
                    System.out.println("Degree 
Scheme:"+ degreeScheme);
                }
            // setter
            public void setForName(String 
forName) {
                this.forName = forName;
            }

            // getter
            public String getForName() {
                return forName;
            }

     }
 }

这是我得到的错误:

TheRealFawcett:lab8 therealfawcett$ javac 
Student.java
Student.java:8: error: illegal start of 
expression
                private String forName;
                ^
Student.java:49: error: class, interface, or enum 
expected
}
^
2 errors
TheRealFawcett:lab8 therealfawcett$ 

我不明白为什么会收到此错误,因为我认为我的主要方法是正确的。

1 个答案:

答案 0 :(得分:1)

Fields属于Class,而不属于方法。 methods也必须处于课程级别。

在您的代码中,所有字段和方法都在main方法中,这是不正确的。

以下代码段显示了正确的版本:

public class Student {
    private static void main(String[] args) {
        Student student = new Student("Charles");
    }

    private String forName;
    private String surName;
    private String studentID;
    private String degreeScheme;

    //This is the Constructor of the 
    public Student(String name) {
        this.forName = forName;
    }

    //Assign the surname of the student 
    public void stuSurname(String stuSurname) {
        surName = stuSurname;
    }

    //Assign the student ID to the student
    public void stuID(String stuID) {
        studentID = stuID;
    }

    //Assign the Degree of the Student
    public void stuDegree(String stuDegree) {
        degreeScheme = stuDegree;
    }

    //Print the student details
    public void printStudent() {
        System.out.println("Forname:" + forName);
        System.out.println("Surename:" + surName);
        System.out.println("Student ID:" + studentID);
        System.out.println("Degree Scheme:" + degreeScheme);
    }

    // setter
    public void setForName(String forName) {
        this.forName = forName;
    }

    // getter
    public String getForName() {
        return forName;
    }
}

要了解有关Java类和对象的更多信息,请遵循this官方教程。