【发布时间】:2019-12-31 02:57:39
【问题描述】:
我正在编写一个程序来查找并显示具有最高 GPA 的学生,以及具有 4 个属性(名字、姓氏、年龄、GPA)的班级中 GPA 最低的学生。
我的代码输出结果为“构建成功”,但解析错误仍然出现,并且没有正确的输出信息。
public class app
{
public static void main(String args[ ])
{
student st1 = new student("Rebecca", "Collins", 22, 3.3);
student st2 = new student("Alex", "White", 19, 2.8);
student st3 = new student("Jordan", "Anderson", 22, 3.1);
student[ ] studentArray = new student[3];
studentArray[0] = st1;
studentArray[1] = st2;
studentArray[2] = st3;
var maxStudent = studentArray[0];
// Start at 1 because we assumed the first student in the array
// has the current max.
//
for (int i = 1; i < studentArray.length; i++)
{
// If the current student has a GPA higher than the student
// with the current max, make the current student the student
// with the current max.
//
if(studentArray[i].gpa > maxStudent.getGpa())
{
boolean max = false;
boolean min;
min = false;
for (student studentArray1 : studentArray) {
boolean gpa = false;
}
System.out.print("The highest GPA is: "+max);
System.out.println();
System.out.print("The lowest GPA is: "+min);
System.out.println();
System.out.println("Name: "+ studentArray[i].firstName + " "+ studentArray[i].lastName);
System.out.println("Age: "+ studentArray[i].age);
System.out.println("GPA: "+ studentArray[i].gpa);
}
}
public class student
{
//class variables
public String firstName;
public String lastName;
public int age;
public double gpa;
public int max = 0;
public int min = 0;
//constructor method
student(String a, String b, int c, double d)
{
firstName = a;
lastName = b;
age = c;
gpa = d;
}
student(String a, String b, int c, double d, int e, int f)
{
firstName = a;
lastName = b;
age = c;
gpa = d;
min = e;
max = f;
}
//a method that returns the student's complete name
String getInfo()
{
return getFirstName() +" " + getLastName() +" " + getMax();
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String fn)
{
firstName = fn;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String ln)
{
lastName = ln;
}
public int getAge()
{
return age;
}
public void setAge(int x)
{
age = x;
}
public double getGpa()
{
return gpa;
}
public void getGpa(double g)
{
gpa = g;
}
public int getMax()
{
return max;
}
public void getMax(int e)
{
max = e;
}
public int getMin()
{
return min;
}
public void getMin(int f)
{
min = f;
}
}
我将不胜感激任何解决错误的见解以及我可以做些什么来使此代码正常工作的解决方案。
【问题讨论】:
-
您是否遇到编译器错误?您正在运行什么命令,确切的错误消息是什么?
-
您发布的代码无法编译;大括号不匹配,Java 中没有关键字
var。我没有看到任何文件 I/O 或代码中可能产生“解析时到达文件末尾”消息的任何位置 -
如果您在文件末尾添加 2 个缺少的
},则当它期待更多源代码时,即它仍在中间时,它不会到达文件末尾解析!!! -
您好,欢迎来到 StackOverflow!您提供的代码不会从文件中读取,因此(可能)无法抛出异常。请检查您提供的代码是否正确,并且您的问题不在于应用程序的其他部分。