该方法返回一些空跳过一些对象。该方法的目标是根据他们的期中'返回分类人员列表。并且' final' ('一个人的中期'紧接着是他的'最终')smb请帮忙修复它?这是代码
public static Exam[] collateExams(Exam[] exams)
{
Exam[] r = new Exam[exams.length];
int index = 0;
for (int i = 0; (i < exams.length) && (index < exams.length/2); i++)
{
if (exams[i].getExamType() == 'm')
{
r[index*2] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), "midterm", exams[i].getScore());
for(int j = 0; (j < exams.length) && (index < exams.length/2); j++)
{
if((exams[j].getExamType() == 'f') && (exams[i].getID() == exams[j].getID()))
{
r[index*2 + 1] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), "final", exams[i].getScore());
}
}
}
index++;
}
return r;
这是输出:
null
null
Bill Gates 6 midterm 90
Bill Gates 6 final 90
James Gosling 3 midterm 100
James Gosling 3 final 100
Sergey Brin 22 midterm 98
null
Dennis Ritchie 5 midterm 94
Dennis Ritchie 5 final 94
Steve Jobs 9 midterm 95
Steve Jobs 9 final 95
这里是未排序的列表,它作为参数传递:
Steve Jobs 9 final 91
Bill Gates 6 midterm 90
James Gosling 3 midterm 100
Sergey Brin 22 midterm 98
Dennis Ritchie 5 midterm 94
Steve Jobs 9 midterm 95
Dennis Ritchie 5 final 100
Jeff Dean 7 midterm 100
Bill Gates 6 final 96
Jeff Dean 7 final 100
Sergey Brin 27 final 97
James Gosling 3 final 100
答案 0 :(得分:0)
你的整个班级都发布了同样的问题,或者你是Joshua mark II。现在,其他变体只表示一个用于循环和其他限制,所以我将忽略所有这些并解决上面混乱的双循环。这将解决上面的循环:
public static Exam[] collateExams(Exam[] exams)
{
Exam[] r = new Exam[exams.length];
int index = 0;
for (int i = 0; i < exams.length ; i++)
{
if (exams[i].getExamType() == 'm')
{
r[index*2] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), 'm', exams[i].getScore());
for(int j = 0; j < exams.length; j++)
{
if((exams[j].getExamType() == 'f') && (exams[i].getID() == exams[j].getID()))
{
r[index*2 + 1] = new Exam(exams[j].getFirstName(), exams[j].getLastName(), exams[j].getID(), 'f', exams[j].getScore());
}
}
index++;
}
}
return r;
}
输出:
Sorted list:
Exam [fn=Bill, ln=Gates, id=6, ex=m, score=90]
Exam [fn=Bill, ln=Gates, id=6, ex=f, score=96]
Exam [fn=James, ln=Gosling, id=3, ex=m, score=100]
Exam [fn=James, ln=Gosling, id=3, ex=f, score=100]
Exam [fn=Dennis, ln=Ritchie, id=5, ex=m, score=94]
Exam [fn=Dennis, ln=Ritchie, id=5, ex=f, score=100]
Exam [fn=Steve, ln=Jobs, id=9, ex=m, score=95]
Exam [fn=Steve, ln=Jobs, id=9, ex=f, score=91]
Exam [fn=Jeff, ln=Dean, id=7, ex=m, score=100]
Exam [fn=Jeff, ln=Dean, id=7, ex=f, score=100]
Exam [fn=Sergey, ln=Brin, id=22, ex=m, score=99]
Exam [fn=Sergey, ln=Brin, id=22, ex=f, score=97]
答案 1 :(得分:0)
[编辑] @Michael,你的代码给我的输出只有一个null:
Sorted list:
Bill Gates 6 midterm 90
Bill Gates 6 final 96
James Gosling 3 midterm 100
James Gosling 3 final 100
Sergey Brin 22 midterm 98
null
Dennis Ritchie 5 midterm 94
Dennis Ritchie 5 final 100
Steve Jobs 9 midterm 95
Steve Jobs 9 final 91
Jeff Dean 7 midterm 100
Jeff Dean 7 final 100
这里是具有toString方法的Exam类:
class Exam {
private String firstName;
private String lastName;
private int ID;
private String examType;
private int score;
public Exam(String firstName, String lastName, int ID, String examType, int score)
{
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID;
this.examType = examType;
this.score = score;
}
public String getFirstName()
{
return this.firstName;
}
public String getLastName()
{
return this.lastName;
}
public int getID()
{
return this.ID;
}
public char getExamType()
{
char examTypeCasted = 0;
examTypeCasted = examType.charAt(0);
return Character.toUpperCase(examTypeCasted);
}
public int getScore()
{
return this.score;
}
public String toString()
{
return this.firstName + " " + this.lastName + " " + this.ID + " " + this.examType + " " + this.score;
}
public boolean equals(Exam e)
{
if(this.equals(e))
return true;
else
return false;
}
}
答案 2 :(得分:0)
这些显示 null 的项目永远不会被初始化,这就是为什么..
public class Example2 {
String array[] = new String[15];
public Example2(){
for(int j=0; j<array.length-1; j++)
array[j]="j";
for(int j=0; j<array.length; j++)
System.out.println(array[j]);
}
public static void main(String[] args){
new Example2();
}
}
这个例子将打印14(“J”)和一个null。为什么?原因数组[15]未初始化。 所以在你的例子中,我确信这些项目显示 null 原因永远不会被初始化。