微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

简单的ArrayList程序-JAVA

我正试图解决这个问题:

Define a class Student with attributes studentID and marks in the module 1030Y. The class Student must also contain a default constructor along with a constructor to initialize an object of type Student with user-defined values,mutator and accessor methods for each attribute and a display method. Write a test program that maintains an ArrayList of Student objects (with IDs in the range 701-799 and marks in the range 0.0 to 100.0). The program will allow the user to input the student id number and the mark for each student. After all the input has been done,the program will display the ids of the students with the highest and the lowest marks.

出于某种原因,我试图获得学生最低和最高分的部分不起作用.

这是我的代码
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);
}
}

程序在读取用户输入后停止.它直到for循环才会发生.

解决方法

您的问题似乎是在使用while语句检查时进入无限循环的事实.使用if语句如下:

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();
        }
    }

但是,这会给你带来另一个问题,那就是你要将标记与id进行比较.您需要检查mark对Mark的值,然后分配id.像这样:

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();
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐