gpt4 book ai didi

java - 我认为我无法正确填充数组

转载 作者:行者123 更新时间:2023-12-02 00:05:14 25 4
gpt4 key购买 nike

我在格式化代码以提供正确的输出时遇到了一些问题。我是 Java 初学者,所以我预计我犯了一些相当愚蠢的错误,但这里是。

我输入的文本文件是:

John Smith  90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
<小时/>

输出文件看起来有点像这样:

John Smith  90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65

Students with excellent grades:
John Smith 90
Barack Obama 95

Students with ok grades:
Al Clark 80
Ann Miller 75
John Miller 65

Students with failure grades:
Sue Taylor 55
George Bush 58


Lowest Grade: Sue Taylor 55
Highest Grade: Barack Obama 95
Average of Grades: 74


Grades in descending order:
John Smith 55
Barack Obama 58
Al Clark 65
Sue Taylor 75
Ann Miller 80
George Bush 90
John Miller 95
<小时/>

我正在寻找的输出是这样的:

输出文件看起来有点像这样:

John Smith  90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65


Students with excellent grades:
John Smith 90
Barack Obama 95


Students with ok grades:
Al Clark 80
Ann Miller 75
John Miller 65


Students with failure grades:
Sue Taylor 55
George Bush 58


Lowest Grade: Sue Taylor 55
Highest Grade: Barack Obama 95
Average of Grades: 74


Grades in descending order:
Barack Obama 95
John Smith 90
Al Clark 80
Ann Miller 75
John Miller 65
George Bush 58
Sue Taylor 55
<小时/>

问题是:

  1. 我需要将姓名(名字和姓氏)打印最低和最高等级。

  2. 成绩的降序明显是升序的,所以需要修复。

  3. 我还需要在每个年级中打印姓名(名字和姓氏)最后按降序排列,现在名字与成绩不匹配。

<小时/>

这是我的代码:

import java.util.Scanner;
import java.io.*;

public class Students
{
public static void main (String[] args) throws IOException
{
String first_name, last_name;
int grade, total=0, count=0;
int min, max;

double average;

int excellentTotal = 0;
int okTotal = 0;
int failureTotal = 0;

Scanner fileInput = new Scanner(new File("students.txt"));
Student st[] = new Student [100];
while (fileInput.hasNext())
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
st[count] = new Student(first_name, last_name, grade);
count++;
total = total + grade;
}

for (int i=0; i<count;i++)
{
System.out.println(st[i]);
}

System.out.println("\nStudents with excellent grades:");
for (int i=0; i<count;i++)
{
if (st[i].grade > 89)
{
System.out.println(st[i]);
excellentTotal += st[i].grade;
}
}

System.out.println("\nStudents with ok grades:");
for (int i=0; i<count;i++)
{
if (st[i].grade >=60 && st[i].grade <=89)
{
System.out.println(st[i]);
okTotal += st[i].grade;
}
}

System.out.println("\nStudents with failure grades:");
for (int i=0; i<count;i++)
{
if (st[i].grade < 60)
{
System.out.println(st[i]);
failureTotal += st[i].grade;
}
}


min = st[0].getGrade();
max = st[0].getGrade();
for (int i = 0; i < count; i++)
{
if (max.grade < st[i].grade)
{
max = st[i];
}
if (min.st[i] > st[i].grade)
{
min = st[i].grade;
}
total = excellentTotal + okTotal + failureTotal;
}

System.out.println();
System.out.println("Lowest Grade: " + min);
System.out.println("Highest Grade: " + max);
System.out.println("Average of Grades: " + total/count);

int t, swap = 0;
do
{
swap = 0;
for (int i=0; i<count-1; i++)
{
if (st[i].getGrade()>st[i+1].getGrade())
{
t=st[i].grade;
st[i].grade=st[i+1].grade;
st[i+1].grade=t;
swap++;
}
}
}
while (swap>0);

System.out.println("\nGrades in descending order: ");

for (int i=0; i<count;i++)
{
System.out.print (st[i] + " ");
System.out.println ();
}
}
static class Student
{
private String fname, lname;
private int grade;

public Student(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}

public int getGrade()
{
return grade;
}

public void setGrade(int grade)
{
this.grade = grade;
}


public String toString()
{
return fname + " " + lname + "\t" + grade;
}


}
}

最佳答案

The issues are:

  1. I need to have the names (first and last) print with the grades for the minimum and maximum.

保留两个字符串,每个字符串包含具有最低和最高成绩的人员姓名,就像您对实际最低和最大值所做的那样。

  1. The descending order of the grades is obviously ascending, so that needs to be fixed.

看起来你已经对数组进行了升序排序(顺便说一句,如果允许的话,请查看Arrays.sort()),所以不要从从前到后,为什么不从数组末尾开始打印呢?这应该按降序打印。

  1. I also need the names (first and last) to print with each grade in the organized descending list at the end.

您的成绩与正确的人不匹配,因为您正在交换数组中每个人的成绩,而不是交换数组中的人员。

请参见此处:

t=st[i].grade; 
st[i].grade=st[i+1].grade;
st[i+1].grade=t;

假设您有 2 个人,Bob 和 Sam,成绩分别为 50 和 75。

它们位于数组中:[Bob(50)][Sam(75)]

使用上面的代码交换它们,最终会得到这样的结果:[Bob(75)][Sam(50)]

解决此问题的更改是微不足道的。我将把它留给你来解决。

关于java - 我认为我无法正确填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023171/

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