gpt4 book ai didi

Java - 使用数组查找/打印最小值/最大值?

转载 作者:行者123 更新时间:2023-11-30 09:08:25 25 4
gpt4 key购买 nike

问题: 我将如何从数组中找到最小值/最大值以及语法是什么样的?

((编辑:开始研究它的外观(在这篇文章的底部),但我的语法不正确))

想在我的主类中创建一个方法来从一组学生中找到成绩最好和最差的学生(并打印),但不确定数组的语法看起来如何。

主.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;
}

}
}

最佳答案

以下情况如何:

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

另一方面,您真的应该考虑使用集合而不是普通数组。特别是因为您事先并不知道学生的数量。

上面的代码可以这样重写:

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
}

关于Java - 使用数组查找/打印最小值/最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23550817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com