我想循环播放我的" print_average_grade"方法并打印ArrayList中所有学生的AVG等级。我尝试了一个" for循环"在主要的"花名册" class并获得一个错误陈述"类型Roster中的方法print_average_grade(String)不适用于参数(Student)"。
我的代码;
学生班:
public class Student {
private String StuID;
private String FName;
private String LName;
private String Email;
private int Age;
private double Grade1;
private double Grade2;
private double Grade3;
public Student (String stuid, String fname, String lname, String email,
int age, double grade1, double grade2, double grade3)
{
this.StuID = stuid;
this.FName =fname;
this.LName = lname;
this.Email = email;
this.Age = age;
this.Grade1 = grade1;
this.Grade2 = grade2;
this.Grade3 = grade3;
}
public String getStuID(){
return this.StuID;}
public String getFName(){
return this.FName;}
public String getLName(){
return this.LName;}
public String getEmail(){
return this.Email;}
public int getAge(){
return this.Age;}
public double getGrade1(){
return this.Grade1;}
public double getGrade2(){
return this.Grade2;}
public double getGrade3(){
return this.Grade3;}
public String setStuID(String newStuID){
return (this.StuID= newStuID);}
public String setFName(String newFName){
return (this.FName= newFName);}
public String setLName(String newLName){
return (this.LName= newLName);}
public String setEmail(String newEmail){
return (this.Email= newEmail);}
public int setAge(int newAge){
return (this.Age= newAge);}
public double setGrade1(double newGrade1){
return (this.Grade1= newGrade1);}
public double setGrade2(double newGrade2){
return (this.Grade2= newGrade2);}
public double setGrade3(double newGrade3){
return (this.Grade1= newGrade3);}
public String toString() {
return String.format("StuID: %s\t First Name: %s\t Last Name: %s\t E-Mail: %s\t Age: %s\t Grade1: %s\t Grade2: %s\t Grade3: %s\t", this.StuID, this.FName, this.LName, this.Email,
this.Age, this.Grade1, this.Grade2, this.Grade3);
}
}
名册等级:
import java.util.ArrayList;
public class Roster {
static ArrayList<Student> studentArray;
public Roster(ArrayList<Student> ar)
{
studentArray=ar;
}
//3.A - Remove student.
public static void remove(String sdId){
Student stud = null;
for(Student s : studentArray){
if(s.getStuID().equals(sdId))
stud = s;}
if(stud != null)
studentArray.remove(stud);
else
System.out.println("ID does not exist.");
}
//3.B - Print All Student Info
public static void print_all(){
System.out.println("");{
for (Student t: studentArray) {
System.out.printf("%s\n",t);}}
}
//3.C - Print Average Grade
public static void print_average_grade(String studentID){
for (Student v : studentArray){
if(v.getStuID().equals(studentID)) {
double total = v.getGrade1() + v.getGrade2() + v.getGrade3();
double average = total / 3;
System.out.println("Student ID#" + studentID + " Grade AVG= " + average);
}
}
}
//3.D - Print invalid E-mails
public static void print_invalid_emails(){
for(Student u : studentArray){
if(u.getEmail().contains("@") && u.getEmail().contains(".") && !u.getEmail().contains(" ")){
continue;}
else{
System.out.println(u.getEmail());}
}
}
public static void main(String[]args){
ArrayList<Student> studentArray = new ArrayList<Student>();
studentArray.add(new Student("1","John","Smith","John1989@gmail.com", 20, 88, 79, 59));
studentArray.add(new Student("2","Susan","Erickson","Erickson_1990@gmailcom", 19, 91, 72, 85));
studentArray.add(new Student("3","Jack","Napoli","The_lawyer99yahoo.com", 19, 85, 84, 87));
studentArray.add(new Student("4","Erin","Black","Erin.black@comcast.net", 22, 91, 98, 82));
studentArray.add(new Student("5","Captain","Planet","PowIsUrs@planet.net", 65, 99, 98, 97));
//Step 2D; print() specific student data example.
System.out.println("");
for (Student a: studentArray) {
System.out.println(a.getStuID());
System.out.println(a.getFName());
System.out.println(a.getLName());}
new Roster(studentArray);
Roster.print_all();
Roster.print_invalid_emails();
//Works fine for one student.
Roster.print_average_grade("1");
/This loop produces the error.
for (Student v : studentArray) {
print_average_grade(v);
}
Roster.remove("3");
Roster.remove("3");
}
}
我正在参加在线Java课程,由于质量问题,材料已多次更改。如果不是这个网站的解释,我会迷路。谢谢..
答案 0 :(得分:3)
你的变量&#39; v&#39;是Student,但您的方法参数是String.Change方法。
public static void print_average_grade(Student v){
double total = v.getGrade1() + v.getGrade2() + v.getGrade3();
double average = total / 3;
System.out.println("Student ID#" + v.getStuID + " Grade AVG= " + average);
}
答案 1 :(得分:1)
您正在将学生对象传递给print_average_grade
而不是ID。
将代码改为:
for (Student v : studentArray) {
print_average_grade(v.getStuID());
}
答案 2 :(得分:1)
您的方法被定义为print_average_grade(String)
您正在将Student
对象传递给此方法,这正是您的编译器告诉您的内容。
您可以通过重载String一个来创建print_average_grade(Student student)
。这可能如下所示。
public static void print_average_grade(Student student) {
print_average_grade(student.getStuID());
}
或者您将循环更改为以下内容,以适合您存在的签名
print_average_grade
方法。
for (Student v : studentArray) {
print_average_grade(v.getStuID());
}
答案 3 :(得分:1)
for (Student v : studentArray) {
print_average_grade(v);
您正在将Student
传递给public static void print_average_grade(String studentID)
,但它需要String
参数。
使用print_average_grade(v.getStuID());
答案 4 :(得分:1)
print_average_grade
接受String的参数。但是,在调用方法时,您传递的是Student类型的对象。
您所要做的就是在调用方法时传递studentId。
喜欢:
for(Student v : studentArray){
print_average_grade(v.studentId);
}
这应该可以解决你的问题:)