如何修复“解析时到达文件末尾”

时间:2019-08-26 23:27:45

标签: java arrays loops class

我正在编写一个程序,以在具有4个属性(名字,姓氏,年龄,GPA)的班级中查找和显示GPA最高的学生,以及GPA最低的学生。

我的代码的输出结果为“ build success”,但是仍然显示分析错误,并且没有正确的输出信息。

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;

     }

 } 

对于能解决该错误的任何见解以及我为使此代码正常工作所能采取的解决方案,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您发布的代码存在很多(可能)问题;您似乎正在使用内部student类;您的花括号不匹配且缩进似乎不一致,并且var关键字在10之前的Java版本中不存在(并且我们不了解您已安装的JDK或配置的项目编译器级别)。您的student不应有单独的minmax字段。

关于具有所有public字段的类的优点,存在一些争论。但是当您同时为所有实现getter和setter时,它的任何可能的优势都被抵消了。

您需要两个循环;一个可以找到minmax,另一个可以显示student。应遵守Java命名约定(类名以大写字母开头)。并且您有一个getGpa(double)应该是二传手。

解决所有这些问题,看起来可能像

public class App {
    public static class Student {
        private String firstName;
        private String lastName;
        private int age;
        private double gpa;

        public Student(String a, String b, int c, double d) {
            firstName = a;
            lastName = b;
            age = c;
            gpa = d;
        }

        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 setGpa(double g) {
            gpa = g;
        }
    }

    public static void main(String[] args) throws Exception {
        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 = { st1, st2, st3 };
        Student maxStudent = studentArray[0];
        Student minStudent = studentArray[0];

        for (int i = 1; i < studentArray.length; i++) {
            if (studentArray[i].getGpa() > maxStudent.getGpa()) {
                maxStudent = studentArray[i];
            }
            if (studentArray[i].getGpa() < minStudent.getGpa()) {
                minStudent = studentArray[i];
            }
        }
        System.out.printf("The highest GPA is: %.1f%n", maxStudent.getGpa());
        System.out.printf("The lowest GPA is: %.1f%n", minStudent.getGpa());
        for (Student s : studentArray) {
            System.out.printf("Name: %s %s%n", s.getFirstName(), s.getLastName());
            System.out.printf("Age: %d%n", s.getAge());
            System.out.printf("GPA: %.1f%n", s.getGpa());
        }
    }
}
我跑过的

生产

The highest GPA is: 3.3
The lowest GPA is: 2.8
Name: Rebecca Collins
Age: 22
GPA: 3.3
Name: Alex White
Age: 19
GPA: 2.8
Name: Jordan Anderson
Age: 22
GPA: 3.1

并且,如果您使用的是Java 8+,则可以通过流式传输main,映射来简化DoubleSummaryStatisticsStudent代码到gpa值,然后收集统计信息。喜欢,

public static void main(String[] args) throws Exception {
    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 = { st1, st2, st3 };
    DoubleSummaryStatistics dss = Arrays.stream(studentArray).mapToDouble(Student::getGpa).summaryStatistics();
    System.out.printf("The highest GPA is: %.1f%n", dss.getMax());
    System.out.printf("The lowest GPA is: %.1f%n", dss.getMin());
    Arrays.stream(studentArray).map(s -> String.format("Name: %s %s%nAge: %d%nGPA: %.1f", //
            s.getFirstName(), s.getLastName(), s.getAge(), s.getGpa())).forEach(System.out::println);
}