从学生名单中找到最低和最高成绩
我必须从学生的list
中找到最小和最大一年级,然后将这些值写入只有名字的输出文件中。
我试图将这个问题描述为值的矩阵,其中每一行都对应学生,每列对应于他们的字段(名称,姓氏等)。我试图将学生读取arrayList
,然后将其浮点等级从arraylist
转移到使用for loop的数组中,但它不起作用。
有人知道该怎么做吗?
public class student implements Comparable<student>{
private String fname;
private String lname;
private String major;
private float grades1;
private float grades2;
private float grades3;
public String getfname() {return fname;}
public String getlname() {return lname;}
private String getmajor() {return major;}
public float getg1() {return grades1;}
public float getg2() {return grades2;}
public float getg3() {return grades3;}
public student(String f,String l,String m,float g1,float g2, float g3) {
fname=f;
lname=l;
major=m;
grades1=g1;
grades2=g2;
grades3=g3;
}
}
public static void main (String[]args) throws IOException
{
PrintWriter pw1 = new PrintWriter ("input.txt");
pw1.println ("Mickey Mouse CS 98.7 67.8 23.5");
pw1.println ("Minnie Mouse ENG 45.6 98.3 94.7");
pw1.println ("Donald Duck NET 56.8 74.2 78.4");
pw1.println ("Bob Builder CS 78.5 89.4 82.5");
pw1.println ("Snow White MAT 56.6 32.4 56.6");
pw1.println ("Hellen Keller CHEM 78.8 23.1 99.6");
pw1.println ("Daffy Duck ENG 67.4 55.5 89.5");
pw1.println ("Fred Flinstone MAT 45.3 87.4 38.9");
pw1.println ("Daffy Duck CS 76.5 22.2 88.5");
pw1.println ("Bugs Bunny NET 68.4 89.7 95.6");
pw1.println ("Winnie Pooh CHEM 77.5 89.4 98.2");
pw1.close ();
PrintWriter pw2 = new PrintWriter ("input2.txt");
pw2.println ("Scrooge McDuck ACC 78.7 77.3 63.5");
pw2.println ("Woody Woodbecker CS 65.6 78.3 84.7");
pw2.println ("Scooby Doo MAT 56.8 78.2 88.4");
pw2.println ("Spider Man CHEM 58.5 99.3 92.5");
pw2.println ("Roger rabbit NET 66.9 39.6 86.6");
pw2.println ("Wonder Woman CHEM 68.4 83.1 69.6");
pw2.println ("Jane Jetson ENG 77.4 85.5 69.5");
pw2.close ();
File in = new File ("input.txt");
java.util.ArrayList < student > list = uploadStudents (in);
float[] gg = new float[list.size()];
for (int i = 0; i < list.size(); i++)
gg[i] = he.getg1();
float min = gg[0];
for (int i = 0; i < gg.length; i++) {
if (gg[i] < gg[0]) {
gg[0] = gg[i];
}
}
}
public static ArrayList < student > uploadStudents (File inputfilename) throws IOException ///
{
java.util.ArrayList < student > student = new java.util.ArrayList <> ();
Scanner sc = new Scanner (inputfilename);
while (sc.hasNext ())
{
//new student object??
student s =
new student (sc.next (), sc.next (), sc.next (), sc.nextFloat (),
sc.nextFloat (), sc.nextFloat ());
student.add (s);
}
return student;
}
//I want a float[] gg array to be the one that starts with 78.88
我尝试使用循环,但显然它只是将所有值复制到gg [0]中;这不是很好,因为我还必须找到阵列列表的每一列的最小值,然后将它们打印在每个等级的最低和最大及其名称的文件中。
I have to find the minimum and maximum first grade from a List
of students and then write those values into an output file with only the first name.
I tried to picture this problem as a matrix of values, where each row corresponds to a Student and each column to their fields (name, last name, etc.). I've tried to read the students into an ArrayList
and then transfer their float grades from the Arraylist
into an array using a for loop but it's not working.
does anyone know how to do this?
public class student implements Comparable<student>{
private String fname;
private String lname;
private String major;
private float grades1;
private float grades2;
private float grades3;
public String getfname() {return fname;}
public String getlname() {return lname;}
private String getmajor() {return major;}
public float getg1() {return grades1;}
public float getg2() {return grades2;}
public float getg3() {return grades3;}
public student(String f,String l,String m,float g1,float g2, float g3) {
fname=f;
lname=l;
major=m;
grades1=g1;
grades2=g2;
grades3=g3;
}
}
public static void main (String[]args) throws IOException
{
PrintWriter pw1 = new PrintWriter ("input.txt");
pw1.println ("Mickey Mouse CS 98.7 67.8 23.5");
pw1.println ("Minnie Mouse ENG 45.6 98.3 94.7");
pw1.println ("Donald Duck NET 56.8 74.2 78.4");
pw1.println ("Bob Builder CS 78.5 89.4 82.5");
pw1.println ("Snow White MAT 56.6 32.4 56.6");
pw1.println ("Hellen Keller CHEM 78.8 23.1 99.6");
pw1.println ("Daffy Duck ENG 67.4 55.5 89.5");
pw1.println ("Fred Flinstone MAT 45.3 87.4 38.9");
pw1.println ("Daffy Duck CS 76.5 22.2 88.5");
pw1.println ("Bugs Bunny NET 68.4 89.7 95.6");
pw1.println ("Winnie Pooh CHEM 77.5 89.4 98.2");
pw1.close ();
PrintWriter pw2 = new PrintWriter ("input2.txt");
pw2.println ("Scrooge McDuck ACC 78.7 77.3 63.5");
pw2.println ("Woody Woodbecker CS 65.6 78.3 84.7");
pw2.println ("Scooby Doo MAT 56.8 78.2 88.4");
pw2.println ("Spider Man CHEM 58.5 99.3 92.5");
pw2.println ("Roger rabbit NET 66.9 39.6 86.6");
pw2.println ("Wonder Woman CHEM 68.4 83.1 69.6");
pw2.println ("Jane Jetson ENG 77.4 85.5 69.5");
pw2.close ();
File in = new File ("input.txt");
java.util.ArrayList < student > list = uploadStudents (in);
float[] gg = new float[list.size()];
for (int i = 0; i < list.size(); i++)
gg[i] = he.getg1();
float min = gg[0];
for (int i = 0; i < gg.length; i++) {
if (gg[i] < gg[0]) {
gg[0] = gg[i];
}
}
}
public static ArrayList < student > uploadStudents (File inputfilename) throws IOException ///
{
java.util.ArrayList < student > student = new java.util.ArrayList <> ();
Scanner sc = new Scanner (inputfilename);
while (sc.hasNext ())
{
//new student object??
student s =
new student (sc.next (), sc.next (), sc.next (), sc.nextFloat (),
sc.nextFloat (), sc.nextFloat ());
student.add (s);
}
return student;
}
//I want a float[] gg array to be the one that starts with 78.88
I've tried to use a for loop, but apparently it just copies all the values into gg[0]; which is not good because I also have to find the minimum of each column of the ArrayList` and then print output them in a file with the min and max of each grade and their name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解了您的问题,您将尝试将您的学生列表表示为一个值的矩阵,其中每一行代表学生和每一列字段。
您无需将学生映射到矩阵即可找到最低和最高等级的矩阵。您已经可以使用拥有的学生清单来实现这一目标。
只需从第二个元素开始用循环的基本
迭代列表即可。第一个学生将被标记为具有最低和最高成绩的学生。然后,在每次迭代中,检查最小学生和最大学生的成绩分别都比您当前迭代的成绩高和低。
最后,如果您想在他们所拥有的三个成绩中找到最低和最高等级的学生,则可以在
student
类中定义方法 get> getminimerGrade()和getmaximumgrade()
分别返回三个中最低和最出色的成绩。学生
部分主要实现了
以前的片段也可以通过流以更简洁的方式重写。
部分主体与流实施
If I understood correctly your problem, you're trying to represent your list of students as a matrix of values where each row represents a student and each column one of their fields.
You don't need to map the students to a matrix to find the ones with the minimum and maximum grade. You can already achieve that with the list of students you have.
Just iterate the list with a basic
for
loop starting from the second element. The first student would be marked as the one with both the minimum and maximum grade. Then, at every iteration, check whether the minimum and maximum students have respectively a higher and lower grade than the ones you're currently iterating.Lastly, in case you want to find the students with the minimum and maximum grade among the three grades they have, you could define in the
Student
class the methodsgetMinimumGrade()
andgetMaximumGrade()
to respectively return the lowest and greatest grades among the three.Student
Partial Main Implementation
The previous snippet could also be re-written in a more concise way with streams.
Partial Main with Stream Implementation