显示java中一系列对象的最高值和最低值
我必须从给定的用户输入中找到得分最高的学生和得分最低的学生。但是我只能得到得分最高的学生,不能从中得到得分最低的学生显示java中一系列对象的最高值和最低值,java,Java,我必须从给定的用户输入中找到得分最高的学生和得分最低的学生。但是我只能得到得分最高的学生,不能从中得到得分最低的学生 public class Student{ public String name; public String id; public int score; public static int n; public Student(String initName,String initID,int initScore){
public class Student{
public String name;
public String id;
public int score;
public static int n;
public Student(String initName,String initID,int initScore){
initName=name;
initID=id;
initScore=score;
}
public Student (){
}
public static void main(String[] args) {
System.out.println("Enter the number of students:");
Scanner s1=new Scanner(System.in);
Student.n=Integer.parseInt(s1.nextLine().trim());
System.out.println("Enter the student name,id and score.");
Scanner s2=new Scanner(System.in);
Student st1=new Student();
Student min=new Student(" "," ",100);
Student max=new Student(" "," ",0);
for(int i=0;i<Student.n;i++){
st1.name=s2.next();
st1.id=s2.next();
st1.score=s2.nextInt();
if(max.score<st1.score){
max.score=st1.score;
max.name=st1.name;
max.id=st1.id;
}
if(min.score>st1.score){
min.name=st1.name;
min.score=st1.score;
min.id=st1.id;
}
}
System.out.println("the highest scoring student: "+max.name);
System.out.println("the lowest scoring student: "+min.name);
}
}
公立班学生{
公共字符串名称;
公共字符串id;
公众智力得分;
公共静态int n;
公立学生(字符串initName、字符串initID、int initScore){
initName=name;
initID=id;
初始分数=分数;
}
公立学生(){
}
公共静态void main(字符串[]args){
System.out.println(“输入学生人数:”);
扫描仪s1=新的扫描仪(System.in);
Student.n=Integer.parseInt(s1.nextLine().trim());
System.out.println(“输入学生姓名、id和分数”);
扫描仪s2=新的扫描仪(System.in);
学生st1=新学生();
学生人数=新学生(“,”,100);
学生人数上限=新学生(“,”,0);
对于(inti=0;iinitScore=score;
请在构造函数中修复上述行
实际上,所有的作业都是错误的:
public Student(String initName、String initID、int initScore){
initName=name;
initID=id;
初始分数=分数;
}
应该改成
public Student(String initName、String initId、int initScore){
name=initName;
id=initId;
分数=初始分数;
}
更好
公立学生(字符串名称、字符串id、整数分数){
this.name=名称;
this.id=id;
这个分数=分数;
}
由于在初始构造中未分配分数
,因此为其分配了一个默认值0
,从而影响min
的计算。(因为它已经在min时假定分数为正值)
它适用于max
计算,因为获得的分数将大于或等于0
,最终将直接更新分数
(假设只允许正分数).我发现您的代码中存在很多问题,而且您似乎真的不知道如何编写代码,因此我建议您先阅读一些Java
教程
现在转到您的代码:
Student
类实现充满了缺陷,没有封装,构造函数中有错误,不知道n
用于什么
public class Student {
private String name;
private String id;
private int score;
public Student(String initName, String initID, int initScore){
this.name = initName;
this.id = initID;
this.score = initScore;
}
// declare getters such as these two
public String getName() {
return this.name;
}
public int getScore() {
return this.score;
}
}
- 你的
main
方法也有很多错误:你有2个扫描器
,你没有初始化足够的学生,你的迭代显然是错误的
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
// get number of students
System.out.println("Enter the number of students:");
int numberOfStudents = s1.nextInt();
// initialize array of students
Student[] array = new Student[numberOfStudents];
for(int i = 0; i < numberOfStudents; i++) {
// get name of the student
System.out.println("Enter the student name:");
String name = s1.nextLine();
// get id of student
System.out.println("Enter the student id:");
String id = s1.nextLine();
// get score of student
System.out.println("Enter the student score:");
int score = s1.nextInt();
array[i] = new Student(name, id, score);
}
// now you have students input
// it's time to find your student
// set the lowest and highest student to first student
Student min = array[0];
Student max = array[0];
for(int i = 1; i < numberOfStudents; i++) {
if(min.getScore() > array[i]) {
min = array[i];
} else if(max.getScore() < array[i]) {
max = array[i];
}
}
System.out.println("the highest scoring student: "+max.getName());
System.out.println("the lowest scoring student: "+min.getName());
}
publicstaticvoidmain(字符串[]args){
扫描仪s1=新的扫描仪(System.in);
//获得学生人数
System.out.println(“输入学生人数:”);
int numberOfStudents=s1.nextInt();
//初始化学生数组
学生[]数组=新学生[numberOfStudents];
for(int i=0;i数组[i]){
min=数组[i];
}else if(max.getScore()
再次仔细检查代码:
public Student(String initName,String initID,int initScore){
initName=name;
initID=id;
initScore=score;
}
你明白了吗…?童车构造师。
代码应该是
public Student(String initName,String initID,int initScore){
name = initName;
id = initID;
score = initScore;
}
请不要使用无意义的字符来增加文本的长度。这些限制是为了迫使您解释问题和代码。tnx抱歉没有注意到Happy的帮助