编辑解决方案
基于forpas代码,我稍微移动了一点,现在最小值显示了预期的值。谢谢大家的帮助。
public static StudentGrade findMinIndex(StudentGrade[] studentGrades)
{
StudentGrade studentGradeMin = studentGrades[0];
for(int i=0; i < studentGrades.length; i++)
{
if(studentGradeMin.getGrade()[0] > studentGrades[i].getGrade()[i])
{
studentGradeMin.getGrade()[0] = studentGrades[i].getGrade()[i];
}
}
System.out.println(studentGradeMin.getGrade()[0]);
return studentGradeMin;
}
编辑:
现在我可以打印出数字了,我在寻找最小值时遇到了问题。我不断收到错误消息: “未为参数类型StudentGrade,StudentGrade定义运算符>”
我需要找到对象数组的最小值,但是我无法正确打印对象。结果将不会显示为整数。 有人可以帮我吗?
这是我的文本文件
6 约翰97.5 吉姆99 凯西34 史蒂夫86.5 史黛西43 信仰88
这是我的方法
//这是构造函数类
public class StudentGrade
{
private String[] names;
private double[] grades; // instance variable
// constructor initializes grades with parameter grades
public StudentGrade(double[] grades, String[] names)
{
this.grades = grades;
this.names = names;
}
// method to set the grades
public void setGrade(double[] grades)
{
this.grades = grades;
}
// method to retrieve the grades
public double[] getGrade()
{
return grades;
}
//Set name
public void setName(String[] names)
{
this.names = names;
}
// method to retrieve the names
public String[] getName()
{
return names;
}
}
//这是我的主班
public static StudentGrade[] initialize(String inputFileName) throws FileNotFoundException
{
Scanner scan = new Scanner(new FileInputStream("grade.txt"));
int size = scan.nextInt();
String[] names = new String[size];
double[] grades = new double[size];
int index = 0;
String[] col = null;
while(scan.hasNextLine())
{
names[index] = scan.next();
grades[index] = scan.nextDouble();
index++;
}
StudentGrade[] studentGrades = new StudentGrade[grades.lenght];
for(int i=0; i< studentGrades; i++)
{
studentGrades[i] = new StudentGrade(grades, names);
}
for(int i=0; i< grades.length; i++)
{
System.out.println(studentGrades[i].getGrade());
}
scan.close();
return studentGrades;
}
//This is to find minimum value
public static StudentGrade findMinIndex(StudentGrade[] studentGrades)
{
StudentGrade studentGradeMax = studentGrades[0];
for(int i=1;i<studentGrades.length;i++)
{
if(studentGrades[i] > studentGradeMax) ==> Error "The operator > is undefined for the argument type(s) StudentGrade, StudentGrade"
{
studentGradeMax = studentGrades[i];
}
}
return studentGradeMax;
}
The code return these values instead of real number
[D@3d4eac69
[D@3d4eac69
[D@3d4eac69
[D@3d4eac69
[D @ 3d4eac69 [D @ 3d4eac69
答案 0 :(得分:0)
您通过以下方式进行打印:
System.out.println(studentGrades[i].getGrade());
但是getGrade()
返回一个数组,因此更改为:
System.out.println(Arrays.toString(studentGrades[i].getGrade()));
或者如果数组仅包含一个数字:
System.out.println(studentGrades[i].getGrade()[0]);
修改
public static StudentGrade findMinGrade(StudentGrade[] studentGrades) {
StudentGrade studentGradeMin = studentGrades[0];
for(int i=1; i < studentGrades.length; i++) {
if(studentGrades[i].getGrade()[0] < studentGradeMin.getGrade()[0]) {
studentGradeMin = studentGrades[i];
}
}
return studentGradeMin;
}
检查一下,希望没有错别字。
这将返回具有最小等级的数组StudentGrade
中的studentGrades
对象。
答案 1 :(得分:0)
最简单的方法是从Object类中重写String toString()方法,因为这是打印对象时使用的默认方法
因此,您将在StudentGrade方法中添加
@Override
public String toString() {
return //your fields
}
这里的问题是,您在StudentGrade中的成员字段是数组,这有点奇怪,所以我想您的toString看起来像这样
@Override
public String toString() {
StringBuilder out = new StringBuilder();
for (String name : names) {
out.append(name);
out.append(", ");
}
for (int i = 0; i < grades.length - 1; i++) {
out.append(grades[i]);
out.append(", ");
}
out.append(grades[grades.length - 1]);
return out.toString();
}