Simple ArrayList program – Java

I am trying to solve this problem:

For some reason, the part I tried to get the lowest and highest scores didn't work

This is my code: student java:

package Number5;

public class Student {
private int studentID;
private float mark;

public Student()
{
    studentID = 0;
    mark = 0;
}

public Student(int id,float marks)
{
    this.studentID = id;
    this.mark = marks;
}

public void setID(int id)
{
    this.studentID = id;
}

public int getID()
{
    return studentID;
}

public void setMark(float marks)
{
    this.mark = marks;
}

public float getMark()
{
    return mark;
}

public void display()
{
    System.out.println("Student ID: "+getID());
    System.out.println("Marks: "+getMark());
}
}

testStudent. java:

package Number5;
import java.util.ArrayList;
import java.util.Scanner;

public class testStudent {

public static void main(String[] args) {
    ArrayList<Student> students = new ArrayList<Student>();
    int id=0; float mark=0;
    Scanner input = new Scanner(system.in);

    do
    {
        System.out.print("Enter the student ID: ");
        id = input.nextInt();
        System.out.print("Enter the marks: ");
        mark = input.nextFloat();
        students.add(new Student(id,mark));
    }
    while(id != 0 || mark != 0);

    int smallest = 9999,largest = -9999;

    for(int i=0; i<students.size(); i++)
    {
        while(smallest > students.get(i).getMark())
        {
            smallest = students.get(i).getID();
        }

        while(largest < students.get(i).getMark())
        {
            largest = students.get(i).getID();
        }
    }

    System.out.println("Smallest is "+smallest);
    System.out.println("Largest is "+largest);
}
}

The program stops after reading user input It doesn't happen until the for loop

Solution

Your problem seems to be the fact that you enter an infinite loop when checking with the while statement Use the if statement as follows:

for(int i=0; i<students.size(); i++)
    {
        if(students.get(i).getMark() < smallest)
        {

            smallest = students.get(i).getID();
        }

        if(students.get(i).getMark() > largest)
        {

            largest = students.get(i).getID();
        }
    }

However, this brings you another problem, that is, you have to compare tags with IDs You need to check the value of mark to mark, and then assign ID. like this:

int largestMark = 0;
inst smallestMark = 9999;
for(int i=0; i<students.size(); i++)
    {
        if(students.get(i).getMark() < smallestMark)
        {
            smallestMark = students.get(i).getMark();
            smallest = students.get(i).getID();
        }

        if(students.get(i).getMark() > largestMark)
        {
            largestMark = students.get(i).getMark();
            largest = students.get(i).getID();
        }
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>