【问题标题】:How can I print the Students info?如何打印学生信息?
【发布时间】:2018-04-26 14:25:09
【问题描述】:

有没有一种方法可以打印我的学生提供的信息? 我创建了一个包含有关课程的所有信息的服务类,我希望以一种非常简单的方式输入我的学生信息。

学生班:

public class student {

    //Fields
    private static String classNumber = "264";
    private static String className = "Transfiguration";
    private static String instructor = "Professor McGonagall";

    private int studentId;
    private String firstname;
    private String lastname;
    private String address;
    private String city;
    private String state;
    private String email;

    //Getters and setters

    static String getClassNumber(){
        return (classNumber);
    }
    static String getClassName(){
        return (className);
    }
    static String getInstructor(){
        return (instructor);
    }
    static void setClassName(String name){
        className = name;
    }

    int getStudentId(){
        return (studentId);
    }
    String getFirstName(){
        return (firstname);
    }
    String getLastName(){
        return (lastname);
    }
    String getAddress(){
        return (address);
    }
    String getCity(){
        return (city);
    }
    String getState(){
        return (state);
    }
    String getEmail(){
        return (email);
    }
    void setFirstName(String first) {
        this.firstname = first;
    }
    void setLastName(String last) {
        this.lastname = last;
    }
    void setAddress(String rua) {
        this.address = rua;
    }
    void setCity(String cidade) {
        this.city = cidade;
    }
    void setEmail(Sring correio) {
        this.email = correio;
     }
     //Contructors
    student(String la) {
        this.firstname = first;
        this.lastname = last;
        this.studentId += 1000;
    }
    student(String la, int id, String first, String last, String rua, String cidade, String correio) {

        this(la);

        this.address = rua;
        this.city = cidade;
        this.email = correio;

     }

    public String toString() {
        String data = "Course: " + classNumber + " " + className + 
                    "\t instructor: " + instructor + 
                    "\t Student Number: " + id + 
                    "\t Student Name: "  + firstname + " " + lastname + 
                    "\t Address: " + rua + cidade + 
                    "\t Email: " + correio;
        return (data);            
    }

}

学生测试课:

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

        System.out.println("Course: " + student.getClassNumber() + student.getClassName());

        student a = new student("Kakashi", "Hatake", "W 69th st", "NY", "khsensei@ninja.com");
        student b = new student("Albus", "Dumbledore", "W 116th st", "NY", "princip-al@hogwarts.com" );
        student c = new student("Hyuk", "Jang", "321 Maple St", "NJ", "jh@actors.com");
        student d = new student("Michael", "Jackson", "543 thriller st", "NY", "mj@singer.com");
        student e = new student("Hamilton", "Alexander", "E 86th st", "NY", "justyouwait@broadway.com");

        String fname = a.firstname;
        System.out.println("First Name: " + fname);

        System.out.println(a.toString());
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
    }
}

我想做的是将所有信息打印在正确的位置,但是我一定忘记了一些非常严重的事情,我收到了错误消息。学生信息是否正确链接到第一堂课?

--

我收到的一些错误消息: “student.java:114: error: class studentTest is public, 应该在一个名为 studentTest.java 的文件中声明” - 对于这个,我之前尝试过,我可以将两个类放在同一个文件中,为什么不现在上班了吗?

有些符号找不到:

符号:变量优先 地点:班级学生 student.java:83:错误:找不到符号 this.lastname = last;

变量id、last、rua、cidade、correio也会发生同样的情况。

构造函数也有错误 构造函数 student.student(String) 不适用 (实际参数列表和形式参数列表的长度不同) 构造函数 student.student(String, int, String, String, String, String) 不适用 student.java:124 错误:找不到适合学生的构造函数(字符串,字符串...)

【问题讨论】:

  • 什么错误信息?这些可能会有所帮助。
  • 你确定构造函数是真的吗?尤其是第一个构造函数。
  • @duffymo 我在帖子中添加了一些错误消息
  • 如果您打算编写 Java,则必须学习如何解释编译器错误消息。相信编译器。如果你坚持一切都很好,你就无法取得进步。
  • 提示:不要写 100 行代码来面对 10,20 个错误。而是只写几行。足以让它编译。然后运行编译器。然后修复错误。这就是你作为新手高效工作的方式。而不是在这里倾倒大量代码并滥用这个社区作为编译器错误翻译服务。

标签: java class methods constructor getter-setter


【解决方案1】:

我添加了 cmets 以更好地指出编译器抱怨的错误所在。这些都是非常常见的编译器消息,因此您应该花时间了解它们所指的内容并修复它们。你会一遍又一遍地看到它们。

public class student { //class names start with an uppercase letter

//Fields
private static String classNumber = "264";
private static String className = "Transfiguration";
private static String instructor = "Professor McGonagall";

private int studentId;
private String firstname;
private String lastname;
private String address;
private String city;
private String state;
private String email;

//Getters and setters

static String getClassNumber(){
    return (classNumber);
}
static String getClassName(){
    return (className);
}
static String getInstructor(){
    return (instructor);
}
static void setClassName(String name){
    className = name;
}

int getStudentId(){
    return (studentId);
}
String getFirstName(){
    return (firstname);
}
String getLastName(){
    return (lastname);
}
String getAddress(){
    return (address);
}
String getCity(){
    return (city);
}
String getState(){
    return (state);
}
String getEmail(){
    return (email);
}
void setFirstName(String first) {
    this.firstname = first;
}
void setLastName(String last) {
    this.lastname = last;
}
void setAddress(String rua) {
    this.address = rua;
}
void setCity(String cidade) {
    this.city = cidade;
}
void setEmail(Sring correio) { //You misspelled the type of the parameter
    this.email = correio;
 }
 //Contructors
student(String la) {
    this.firstname = first; //The parameter list to this constructor 
                            //only contains 'la', 'first' does not exist
    this.lastname = last; //The parameter list to this constructor 
                          //only contains 'la', 'last' does not exist
    this.studentId += 1000;
}
student(String la, int id, String first, String last, String rua, String cidade, String correio) {

    this(la);

    this.address = rua;
    this.city = cidade;
    this.email = correio;

 }

public String toString() {
    String data = "Course: " + classNumber + " " + className + 
                "\t instructor: " + instructor + 
                "\t Student Number: " + id + //id is the parameter in the 
                                             //constructor, not the private field name
                "\t Student Name: "  + firstname + " " + lastname + 
                "\t Address: " + rua + cidade + //rua and cidade are parameters in the 
                                             //constructor, not the private field names
                "\t Email: " + correio; //correio is the parameter in the 
                                        //constructor, not the private field name
    return (data);            
}

}

学生测试课:

public class studentTest { //classes start with a capital letter and belong in seprate files
public static void main (String [] args) {

    System.out.println("Course: " + student.getClassNumber() + student.getClassName());

    //Student's constructor requires 7 arguments of type String, int, String, String, String, String, String
    student a = new student("Kakashi", "Hatake", "W 69th st", "NY", "khsensei@ninja.com");
    student b = new student("Albus", "Dumbledore", "W 116th st", "NY", "princip-al@hogwarts.com" );
    student c = new student("Hyuk", "Jang", "321 Maple St", "NJ", "jh@actors.com");
    student d = new student("Michael", "Jackson", "543 thriller st", "NY", "mj@singer.com");
    student e = new student("Hamilton", "Alexander", "E 86th st", "NY", "justyouwait@broadway.com");

    String fname = a.firstname; //firstname is a private field
    System.out.println("First Name: " + fname);

    System.out.println(a.toString());
    System.out.println(b);
    System.out.println(c);
    System.out.println(d);
    System.out.println(e);
}
}

【讨论】:

    【解决方案2】:

    代码中的问题出在这一行:

    String fname = a.firstname;

    您实际上是在尝试访问私有字段:

    String fname = a.getFirstName();


    另一个问题是构造函数:

    student(String la) {
            this.firstname = first;
            this.lastname = last;
            this.studentId += 1000;
        }
    

    此上下文中不存在 first 和 last 变量。

    【讨论】:

    • 没错,我错过了“()”。但是,我仍然收到我刚刚在主帖中添加的错误消息。
    • 一方面,你不能在同一个文件中有两个顶级类。您必须将测试与实际代码分开。无论如何,这实际上被认为是一种很好的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2020-12-28
    • 2021-06-28
    • 2013-12-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多