如何创建计算学生成绩的java程序

时间:2015-07-16 09:55:15

标签: java algorithm oop

我被要求创建一个具有以下规范的java OOP程序:

  

学生成绩名称:字符串ID:   字符串数学成绩:双英语成绩:   双重科学等级:双倍平均():   double printInfo():void

说明是:

  

1)创建学生成绩等级

     

2)创建一个10学生的数组

     

3)使用键盘输入成绩信息

     

4)打印学生名单(姓名和平均数)

我的问题是每次运行公共课 Exception in thread "main" java.lang.NullPointerException at javaDay3.StudentGradeApp2.main(StudentGradeApp2.java:15) 时,它总是给我这个错误

public static void printTheStudentDetails (StudentGrade info) {

现在我不知道该放在这个部分:

StudentGrade
请帮助我

我的代码是: package javaDay3; public class StudentGrade { String name; String id; double mathgrade; double enggrade; double scigrade; public void printInfo() { System.out.println("Math = " + mathgrade); System.out.println("English = " + enggrade); System.out.println("Science = " + scigrade); System.out.println( "Average = " + average()); } public double average () { return ((mathgrade + enggrade + scigrade) / 3); } public void printCompleteInformation() { System.out.println("Name = " + name); System.out.println("ID = " + id); System.out.println("Math = " + mathgrade); System.out.println("English = " + enggrade); System.out.println("Science = " + scigrade); System.out.println( "Average = " + average()); } public void printNeed () { System.out.println("Name = " + name); System.out.println( "Average = " + average()); } } 班级:

    package javaDay3;

    import java.util.Scanner;

    public class StudentGradeApp {

        public static void main(String[] args) {
            // TODO Auto-generated method stub

            StudentGrade stud1 = new StudentGrade ();
            stud1.name = "SpongeBop SquarePants";
            stud1.id = ("Student 1");
            stud1.mathgrade = 72;
            stud1.enggrade = 80;
            stud1.scigrade = 90;
            stud1.average();

            StudentGrade stud2 = new StudentGrade ();
            stud2.name = "Patrick Star";
            stud2.id = ("Student 2 ");
            stud2.mathgrade = 72;
            stud2.enggrade = 85;
            stud2.scigrade = 91;
            stud2.average();

            StudentGrade stud3 = new StudentGrade ();
            stud3.name = "Squidward Tentacles";
            stud3.id = ("Student 3" );
            stud3.mathgrade = 90;
            stud3.enggrade = 85;
            stud3.scigrade = 95;
            stud2.average();

            StudentGrade stud4 = new StudentGrade ();
            stud4.name = "Eugene H. Krabs";
            stud4.id = ("Student 4");
            stud4.mathgrade = 95;
            stud4.enggrade = 85;
            stud4.scigrade = 95;
            stud4.average();

            StudentGrade stud5 = new StudentGrade ();
            stud5.name = "Sandy Cheeks";
            stud5.id = ("Student 5");
            stud5.mathgrade = 75;
            stud5.enggrade = 75;
            stud5.scigrade = 95;
            stud5.average();

            StudentGrade stud6 = new StudentGrade ();
            stud6.name = "Gary the Snail";
            stud6.id = ("Student 6");
            stud6.mathgrade = 75;
            stud6.enggrade = 74;
            stud6.scigrade = 95;
            stud6.average();

            StudentGrade stud7 = new StudentGrade ();
            stud7.name = "Sheldon J Plankton";
            stud7.id = ("Student 7 ");
            stud7.mathgrade = 79;
            stud7.enggrade = 76;
            stud7.scigrade = 75;
            stud7.average();

            StudentGrade stud8 = new StudentGrade ();
            stud8.name = "Larry The Lobster";
            stud8.id = ("Student 8");
            stud8.mathgrade = 79;
            stud8.enggrade = 76;
            stud8.scigrade = 75;
            stud8.average();

            StudentGrade stud9 = new StudentGrade ();
            stud9.name = "King Neptune";
            stud9.id = ("Student 9 ");
            stud9.mathgrade = 79;
            stud9.enggrade = 96;
            stud9.scigrade = 75;
            stud9.average();

            StudentGrade stud10 = new StudentGrade ();
            stud10.name = "Pearl Krabs";
            stud10.id = ("Student 10 ");
            stud10.mathgrade = 79;
            stud10.enggrade = 76;
            stud10.scigrade = 75;
            stud10.average();

            /*stud1.printInfo();
            System.out.println(" ");
            stud2.printInfo();
            System.out.println(" ");
            stud3.printInfo();
            System.out.println(" ");
            stud4.printInfo();
            System.out.println(" ");
            stud5.printInfo();
            System.out.println(" ");
            stud3.printInfo();
            System.out.println(" ");
            stud6.printInfo();
            System.out.println(" ");
            stud7.printInfo();
            System.out.println(" ");
            stud8.printInfo();
            System.out.println(" ");
            stud9.printInfo();
            System.out.println(" ");
             stud10.printInfo(); */
        }
        public static void printTheStudentDetails (StudentGrade info) {
    }
    }

StudentGradeApp课程:

StudentGradeApp2

import java.util.Scanner; public class StudentGradeApp2 { public static void main(String[] args) { // TODO Auto-generated method stub StudentGrade studgrad [] = new StudentGrade [10]; Scanner scanner = new Scanner (System.in); for(int i = 0; i <= studgrad.length; i++) { System.out.print("Enter Math Grade = "); studgrad[i].mathgrade = scanner.nextInt(); System.out.print("Enter English Grade = "); studgrad[i].enggrade = scanner.nextInt(); System.out.print("Enter Science Grade = "); studgrad[i].scigrade = scanner.nextInt(); } for(StudentGrade info:studgrad) info.printInfo(); scanner.close(); } } 上课:

Hibernate

3 个答案:

答案 0 :(得分:2)

java.lang.NullPointerException的已发布代码存在以下问题:

  • 您在studgrad[i].mathgrade获得null.mathgrade。它相当于调用mathgrade, enggrade, scigrade
  • {li> StudentGrade scanner.nextInt()中的For loop condition是双倍的,您正在使用package javaDay3; import java.util.Scanner; public class StudentGradeApp2 { public static void main(String[] args) { // TODO Auto-generated method stub StudentGrade studgrad[] = new StudentGrade[10]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < studgrad.length; i++) { StudentGrade sg = new StudentGrade(); System.out.print("Enter Math Grade = "); sg.mathgrade = Double.parseDouble(scanner.nextLine()); System.out.print("Enter English Grade = "); sg.enggrade = Double.parseDouble(scanner.nextLine()); System.out.print("Enter Science Grade = "); sg.scigrade = Double.parseDouble(scanner.nextLine()); studgrad[i] = sg; } for (StudentGrade info : studgrad) info.printInfo(); scanner.close(); } }
  • between不正确

PFB更正实施:

\DateTime

答案 1 :(得分:1)

  

现在我不知道该放在这个部分:

     

public static void printTheStudentDetails(StudentGrade info){

您需要一个列表来添加所有学生成绩

org.hibernate.QueryException:
could not resolve property: pet.name of: Hero

每次创建新的StudentGrade对象时,都会将其添加到列表中。

在StudentGrade课程中override List<> studentList = new ArrayList<StudentGrade>(); studentList.add(stud1); studentList.add(stud2); 方法

现在,在您的方法中,printTheStudentDetails传递学生列表toString()

studentList

答案 2 :(得分:0)

调试您遇到问题的代码。你会发现你的对象是null。不要忘记将数据添加到列表中,以便可以使用它们。