Java arraylist未打印正确的数据
试图返回最高分数,但返回了错误的分数…不确定问题是什么,如果我将arraylist中的第一个对象设置为最高分数,然后将其与最高分数进行比较Java arraylist未打印正确的数据,java,arraylist,Java,Arraylist,试图返回最高分数,但返回了错误的分数…不确定问题是什么,如果我将arraylist中的第一个对象设置为最高分数,然后将其与最高分数进行比较 public String findHighest () { Student newStu; Student s; int highest; s=collegeList.get(0); highest=s.getQuizScore(); for (int i=1; i<collegeList.size() ;i++ ) { newStu=
public String findHighest () {
Student newStu;
Student s;
int highest;
s=collegeList.get(0);
highest=s.getQuizScore();
for (int i=1; i<collegeList.size() ;i++ ) {
newStu=collegeList.get(i);
if (highest>newStu.getQuizScore()){
highest=newStu.getQuizScore();
return newStu.toString();
}
}
公共字符串findHighest(){
学生新闻;
学生证;
int最高;
s=合议者。获取(0);
最高=s.getQuizScore();
对于(int i=1;inewStu.getQuizScore()){
最高=newStu.getQuizScore();
return newStu.toString();
}
}
}
公共字符串findHighest(){
学生新闻;
学生证;
int最高;
s=合议者。获取(0);
最高=s.getQuizScore();
对于(int i=1;i
//试过这个,它一直在说newStu可能没有被初始化…你的情况似乎发生了逆转:
if (highest>newStu.getQuizScore()){
将其更改为:
if (highest<newStu.getQuizScore()){
如果(最高您的病情似乎发生逆转:
if (highest>newStu.getQuizScore()){
将其更改为:
if (highest<newStu.getQuizScore()){
if(最高您的for
循环应如下所示:
for (int i=1; i<collegeList.size() ;i++ ) {
newStu=collegeList.get(i);
if (highest<newStu.getQuizScore()){
highest=newStu.getQuizScore();
}
}
return newStu.toString(); //return the value after for loop
for(int i=1;i您的for
循环应该是这样的:
for (int i=1; i<collegeList.size() ;i++ ) {
newStu=collegeList.get(i);
if (highest<newStu.getQuizScore()){
highest=newStu.getQuizScore();
}
}
return newStu.toString(); //return the value after for loop
用于(int i=1;i第一件事是在0处开始for循环的索引。然后您应该将您的返回放在循环之后,因为当返回到达时,循环将自动停止。第一件事是在0处开始for循环的索引。然后您应该将您的返回放在循环之后,因为当retu达到rn。您将返回迭代中的第一个最高分数
if (highest>newStu.getQuizScore()){ //here the comparison problem
highest=newStu.getQuizScore();
return newStu.toString(); // here returning the first highest score in the iteration.
}
试试下面的代码
public String findHighest () {
Student newStu;
Student s;
int highest;
s=collegeList.get(0);
highest=s.getQuizScore();
for (int i=1; i<collegeList.size() ;i++ ) {
f (highest<collegeList.get(i)){
highest=newStu.getQuizScore();
newStu=collegeList.get(i)
}
}
return newStu.toString();
}
公共字符串findHighest(){
学生新闻;
学生证;
int最高;
s=合议者。获取(0);
最高=s.getQuizScore();
对于(inti=1;i您将返回迭代中的第一个最高分数
if (highest>newStu.getQuizScore()){ //here the comparison problem
highest=newStu.getQuizScore();
return newStu.toString(); // here returning the first highest score in the iteration.
}
试试下面的代码
public String findHighest () {
Student newStu;
Student s;
int highest;
s=collegeList.get(0);
highest=s.getQuizScore();
for (int i=1; i<collegeList.size() ;i++ ) {
f (highest<collegeList.get(i)){
highest=newStu.getQuizScore();
newStu=collegeList.get(i)
}
}
return newStu.toString();
}
公共字符串findHighest(){
学生新闻;
学生证;
int最高;
s=合议者。获取(0);
最高=s.getQuizScore();
对于(inti=1;i有几种方法可以实现这一点
第一种也是最直接的方法是修复逻辑错误:返回此时找到的最大元素。不等式也被颠倒
将此更改为:
for (int i = 1; i < collegeList.size(); i++) {
newStu = collegeList.get(i);
if (highest < newStu.getQuizScore()){
highest = newStu.getQuizScore();
}
}
return newStu.toString();
…然后,构建树集
:
SortedSet<Student> orderedStudents = new TreeSet<>();
SortedSet orderedStudents=new TreeSet();
…您可以像列表一样将元素放入此集合中。您最大的元素现在位于集合的端,可以通过简单的last()
调用来访问。有几种方法
第一种也是最直接的方法是修复逻辑错误:返回此时找到的最大元素。不等式也被颠倒
将此更改为:
for (int i = 1; i < collegeList.size(); i++) {
newStu = collegeList.get(i);
if (highest < newStu.getQuizScore()){
highest = newStu.getQuizScore();
}
}
return newStu.toString();
…然后,构建树集
:
SortedSet<Student> orderedStudents = new TreeSet<>();
SortedSet orderedStudents=new TreeSet();
…您可以像放入列表一样将元素放入此集合中。您最大的元素现在位于集合的端,可以通过简单的last()
调用来访问。这里有一个主意。根据分数对列表进行排序,然后根据排序顺序弹出最后一个(或第一个):这是一个主意。根据分数对列表进行排序,然后弹出最后一个(或根据排序顺序弹出第一个):Pkeeps给我一个错误,说变量newStu可能尚未初始化……因此将其初始化为null。一直给我一个错误,说变量newStu可能尚未初始化……因此将其初始化为null。