有没有办法用我提供的信息打印我的学生? 我创建了一个服务类,其中包含有关课程的所有信息,我希望以非常简单的方式输入学生信息。
学生班:
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);
}
}
studentTest课程:
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:错误:类studentTest是公共的,应该在名为studentTest.java的文件中声明” - 对于这一个,我之前尝试过,我可以将两个类放在同一个文件中,为什么不会现在正在工作?
无法找到某些符号:
symbol:变量首先 地点:班级学生 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错误:没有为student找到合适的构造函数(String,String ...)
答案 0 :(得分:1)
代码中的问题在于这一行:
String fname = a.firstname ;
您实际上想要做的是基本上尝试访问私有字段:
String fname = a.getFirstName();
另一个问题是构造函数:
student(String la) {
this.firstname = first;
this.lastname = last;
this.studentId += 1000;
}
在此上下文中不存在第一个和最后一个变量。
答案 1 :(得分:0)
我添加了注释以更好地指出编译器抱怨的错误。这些都是非常常见的编译器消息,因此您应该花时间了解它们所指的内容并修复它们。你会一遍又一遍地看到它们。
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);
}
}
studentTest课程:
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);
}
}