gpt4 book ai didi

java - 如何找到两个人的最佳答案

转载 作者:行者123 更新时间:2023-12-01 19:03:06 25 4
gpt4 key购买 nike

我正在尝试找到从控制台输入的两个最大数字。我找到了第一个,但第二个的解决方案不起作用。程序正在编译并运行。这是代码。

import java.util.Scanner;

public class FindingSecondHighestScore_4_09 {

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

double max = 1;
double score2 = 0;
String firstName = "";
String secondName = null;
System.out.println("Enter number of students: ");
int x = input.nextInt();
while(x > 0)
{
System.out.println("Enter Sudent's name");
String name = input.next();
System.out.println("Enter Student's score");
double score = input.nextDouble();

//find max
if(score > max)
{
max = score;
firstName = name;
}

//find second max
if(max < score2 || score < score2)
{
max = score2;
score = score2;
}
else if(max > score2 && score2 < score)
{
score2 = score;
secondName = name;
}


x--;
}
System.out.println("The student: " + firstName + " has the greatest score: " + max);
System.out.println("Second studemt " + secondName + " with second results: " + score2);

}

}

最佳答案

这是一个更复杂的实现(我今天的起床练习):

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TopScores {

private static final int TOP_SELECTION_SIZE = 2;

public static class Student {
private final String name;
private double score;

public Student(String name) {
if (name == null || name.length() == 0) {
throw new IllegalArgumentException("Name cannot be empty");
}
this.name = name;
}

public String getName() {
return name;
}

public double getScore() {
return score;
}

public void setScore(String score) {
try {
this.score = Double.parseDouble(score);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Illegal score: " + score);
}
}

@Override
public String toString() {
return String.format("%s with score %s", name, score);
}
}

public static void main(String[] args) {

List<Student> students = new ArrayList<TopScores.Student>();
System.out.println("Please enter students. Press <RETURN> to stop.");
Scanner input = new Scanner(System.in);
boolean enteringData = true;
while (enteringData) {
try {
System.out.print("Enter student's name: ");
Student student = new Student(input.nextLine());
System.out.print("Enter student's score: ");
student.setScore(input.nextLine());
for (int i = 0; i < students.size(); i++) {
if (student.getScore() > students.get(i).getScore()) {
students.add(i, student);
break;
}
}
if (students.size() == 0) {
students.add(student);
}
} catch (IllegalArgumentException e) {
enteringData = false;
}
}

int studentsToDisplay = Math.min(TOP_SELECTION_SIZE, students.size());
if (studentsToDisplay > 0) {
System.out.println("Top students:");
for (int i = 0; i < studentsToDisplay; i++) {
System.out.println("* " + students.get(i));
}
} else {
System.out.println("No students to display");
}
}
}

我创建了一个单独的学生类,它保存姓名和分数,验证输入并为一名学生创建显示格式。

为了确定最高分,我通过将每个新学生添加到正确的位置,将所有输入的学生排序在列表中。

用户不必事先输入学生人数,但可以通过输入空行(或无效分数)来终止数据输入。

数据输入完成后,将打印所需的得分最高的学生人数。

这种方式更加灵活;打印前 3 名或前 10 名学生只需更改 TOP_SELECTION_SIZE 的值即可。

最重要的收获:尽可能在类里面(在本例中为学生)思考,并将合理的责任委派给每个类(class)。

关于java - 如何找到两个人的最佳答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11446268/

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