好的,所以主要的方法是在我的书中给我的。我只需要使用setter和getter,并创建一个determineTypeOfStudent方法。目标是显示他们的姓名,年龄,然后显示他们可能的成绩。
出现的错误如下:
System.out.println("Age ", + student1.getAge());
它没有找到适合println(String,int)的方法,后面跟着一段说"不适用"
System.out.println("Type of Student: " + student1.determineTypeOfStudent());
这个说"方法中的defineTypeOfStudent类不能应用于给定的类型; 必需:int 发现:非论点 原因:实际和正式的参数列表在长度上有所不同
环绕......
然后这两个:
System.out.println("Type of Student: " + student2.determineTypeOfStudent());
System.out.println("Type of Student: " + student1.determineTypeOfStudent());
与前一个人相同。
我对这些东西很陌生,所以如果你们纠正我并给我一个解释感谢,我会很感激。
public class StudentClient {
public static void main(String[] args) {
Student student1 = new Student("Bob", 15);
Student student2 = new Student("Jan", 13);
System.out.println("Name: " + student1.getName());
System.out.println("Age ", + student1.getAge());
System.out.println("Type of Student: " + student1.determineTypeOfStudent());
System.out.println("\n" + student2.fullString());
System.out.println("Type of Student: " + student2.determineTypeOfStudent());
student1.setName("Ted");
student1.setAge(35);
System.out.println("\n" + student1.fullString());
System.out.println("Type of Student: " + student1.determineTypeOfStudent());
}
------- CLASS FILE -----------
public class Student {
private String studentName;
private int studentAge;
public Student(String newName, int newAge) {
setName(newName);
setAge(newAge);
}
public void setName(String newName) {
studentName = newName;
}
public String getName() {
return studentName;
}
public void setAge(int newAge) {
if (newAge > 0) {
studentAge = newAge;
} else {
System.out.println("Age cannot be less than zero");
}
}
public int getAge() {
return studentAge;
}
public String fullString() {
return (studentName + studentAge);
}
public static String determineTypeOfStudent(int studentAge){
if (studentAge > 0 || studentAge <= 4)
return "Preschool";
else if (studentAge == 5)
return "Kindergarten";
else if (studentAge >= 6 || studentAge <= 10)
return "Elementary School";
else if (studentAge >= 11 || studentAge <= 13)
return "Middle School";
else if (studentAge >= 14 || studentAge <= 17)
return "High School";
else if (studentAge >= 18)
return "College";
else
return "Invalid";
}
}
答案 0 :(得分:0)
你应该改变:
System.out.println("Age ", + student1.getAge());
到
System.out.println("Age " + student1.getAge());
System.out.println
只需一个参数。
对于determineTypeOfStudent
- 你没有参数调用它,但它声明接受一个int参数。此外,您应该考虑是否真的希望该方法是静态的。
答案 1 :(得分:-1)
第一个不好,因为println只接受一个参数。第二个不起作用,因为您无法添加String和int。您应首先在String上调用print,然后在int上打印。
System.out.print("Age ");
System.out.println(student1.getAge());