问题: 如何从数组中查找min / max以及语法是什么样的?
((编辑:开始处理它可能是什么样子(在这篇文章的底部),但我没有正确的语法))
想在我的主课程中创建一个方法,以便从一系列学生中找到具有最佳和最差成绩(和打印)的学生,而不确定语法在数组中的外观。
Main.java
import java.util.*;
import java.io.*;
public class Main
{
private static int i=0;
public static void main (String[] args) throws IOException
{
Student[] arrStudents = new Student[7];
Scanner fileInput = new Scanner(new File("students.txt"));
while (fileInput.hasNext())
{
String firstName = fileInput.next();
String lastName = fileInput.next();
int grade = fileInput.nextInt();
arrStudents[i] = new Student(firstName, lastName, grade);
i++;
}
getExcellentStudents(arrStudents);
getOKStudents(arrStudents);
getFailStudents(arrStudents);
System.out.println();
System.out.println("Total Number of Students: " + arrStudents.length);
}
public static void getExcellentStudents(Student[] arrStudents) throws IOException {
System.out.println("Excellent Students: ");
for(int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (grade > 89) {
System.out.println(arrStudents[i]);
}
}
System.out.println();
}
public static void getOKStudents(Student[] arrStudents) throws IOException {
System.out.println("OK Students: ");
for(int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (grade > 60 && grade < 90) {
System.out.println(arrStudents[i]);
}
}
System.out.println();
}
public static void getFailStudents(Student[] arrStudents) throws IOException {
System.out.println("Failure Students: ");
for(int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (grade < 61) {
System.out.println(arrStudents[i]);
}
}
System.out.println();
}
Student.java
public class Student {
private String firstName, lastName;
private int grade;
public Student (String firstName, String lastName, int grade)
{
this.firstName = firstName;
this.lastName = lastName;
this.grade = grade;
}
public String toString()
{
return firstName + " " + lastName + " " + grade;
}
public int getGrade()
{
return grade;
}
}
students.txt
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
以下是输出的外观(所以我现在错过了最后两行):
Excellent Students:
John Smith 90
Barack Obama 95
OK Students:
Al Clark 80
Ann Miller 75
John Miller 65
Failure Students:
Sue Taylor 55
George Bush 58
Total Number of Students: 7
Student With Highest Grade: Barack Obama 95
Student With Lowest Grade: Sue Taylor 55
此处编辑比在评论中发布更容易。我更新了帖子,使其更清楚我要问的是什么。
这是我到目前为止找到最大值的内容,但我仍然对确切的内容感到困惑。
public void outputMaxMin(Student[] arrStudents) throws IOException {
Student bestStudent;
Student worstStudent;
student = new Student();
for (int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (bestStudent == null && worstStudent == null) {
bestStudent = student;
worstStudent = student;
continue;
}
if (grade > bestStudent.grade){
bestStudent = student;
}
if (grade < worstStudent.grade){
worstStudent = student;
}
}
}
答案 0 :(得分:1)
要获得最高和最低等级,只需循环遍历数组中的所有项目 将2个变量保持为最高和最低。
int highest = 0;
int lowest = 100;
Student high = arrStudents[0];
Student low = arrStudents[0];
for (int index = 0; index < arrStudents.length; index++)
{
if (arrStudents[index].getGrade() > highest)
{
highest = arrStudents[index].getGrade();
high = arrStudents[index];
}
else if (arrStudents[index].getGrade() < lowest)
{
lowest = arrStudents[index].getGrade();
low = arrStudents[index];
}
}
System.out.println("Student with the highest grade: " + high);
System.out.println("Student with the lowest grade: " + low);
然后按照您喜欢的方式打印出来。 希望这会有所帮助。
答案 1 :(得分:1)
以下内容如何:
// assume students.length > 0
static void printMinMax(Student[] students) {
Student min = students[0];
Student max = students[0];
for (Student student: students) {
if (student.getGrade() > max.getGrade())
max = student;
if (student.getGrade() < min.getGrade())
min = student;
}
System.out.println("Best student: " + max);
System.out.println("Worst student: "+ min);
}
另一方面,您应该考虑使用Collections而不是plain数组。 特别是因为你事先并不真正了解学生人数。
以上代码可以像这样重写:
static void printMinMax(List<Student> students) {
Comparator<Student> comparator = new Comparator<>() {
@Override public int compare(Student s1, Student s2) {
return s1.getGrade() - s2.getGrade();
}
};
Student max = Collections.max(students, comparator);
Student min = Collections.min(students, comparator);
// print stuff
}
或更短的使用java 8 lambdas:
static void printMinMax(List<Student> students) {
Comparator<Student> comparator = (s1, s2) -> s1.getGrade() - s2.getGrade();
Student max = Collections.max(students, comparator);
Student min = Collections.min(students, comparator);
// print stuff
}
答案 2 :(得分:0)
您需要遍历数组并保留max和min的值
Student[] arrStudents = new Student[7];
Student max=arrStudents[0];
Student min=arrstudents[1];
for (int i=0; i < arrStudents.length(); i++){
if arrStudents[i].getGrade() >max.getGrade(){
max = arrStudents[i];
}
else if arrStudents[i].getGrade() <min.getGrade(){
min =arrStudents[i];
}
}