Java - 对象无法存储数据

时间:2016-12-27 16:49:33

标签: java

我爸爸和我把它缩小到对象无法存储的数据。另外,我是java的新手。代码应该在控制台中打印名字,姓氏,等级和等级平均值。我认为问题出在公共双getCalcGraeAverage(),但如果错了请纠正我。

import java.util.Scanner;
/**
 * Write a description of class Student here.
 * 
 * @author XXXX
 * @version XXXX
 */
public class Student
{
    String firstName;
    String lastName;
    int gradeLevel;
    double gradeAverage;
    int totalAssignments;
    double totalPoints;

      /**
     * Create a new student with student's name, grade, and average
     */
    public Student(String newFirstName, String newLastName, int newGradeLevel, double newGradeAverage)
    {
        firstName = newFirstName;
        lastName = newLastName;
        gradeLevel = newGradeLevel;
        gradeAverage = newGradeAverage = 0.0;
    }

      /**
     * Return the student's first name. 
     */
    public String getFirstName()
    {
        return firstName;
    }

      /**
     * Return the student's last name. 
     */
    public String getLastName()
    {
        return lastName;
    }

      /**
     * Return the grade level of the student. 
     */
    public int getGradeLevel()
    {
        return gradeLevel;
    }

         /**
     * Calculates grade average. 
     */
    public double getCalcGradeAverage()
    {

       double gradeAverage = totalAssignments / totalPoints;
       return gradeAverage;
    }

    public static void main (String[] args)
    { 
        Student student1 = new Student ("XXXX", "XXXX", 11, 0.0);

        System.out.println("The student's first name is: " + student1.getFirstName());
        System.out.println("The student's last name is: " + student1.getLastName());
        System.out.println("The student's grade level is: " + student1.getGradeLevel());

        System.out.println("Please enter the total assignment points the student has earned: ");

        Scanner input = new Scanner(System.in);
        Double totalAssignments  = input.nextDouble();

        System.out.println("Please enter the number of assignments given: ");

        double totalpoints  = input.nextDouble();

        System.out.println(student1.getFirstName() + " " + student1.getLastName() +  " average grade is" + student1.getCalcGradeAverage());
    }
}

2 个答案:

答案 0 :(得分:1)

在您的代码中,您是:

  1. 创建Student student1对象
  2. totalAssignments
  3. 阅读totalpointsSystem.in
  4. 致电student1.getCalcGradeAverage()
  5. 在第2步和第3步之间,您必须将totalAssignments的{​​{1}},totalpoints字段设置为您读取的值,否则它们将保留默认值零。 E.g。

    student

    此外,由于student1.totalAssignments = totalAssignments; student1.totalpoints = totalpoints; 的类型为totalAssignments,因此您可能希望将其读作:

    int

答案 1 :(得分:0)

编写代码时,为类范围声明学生类的变量。

int totalAssignments;
double totalPoints;

方法中使用了这些类范围变量:getCalcGradeAverage()  学生的总分数和学生的总分数用于此方法

创建新学生时,这些变量等于零,因为它不受构造函数中值的影响。

写入时主要方法中的

Double totalAssignments  =

使用方法范围声明一个名为“totalAssignments”的新变量。当方法结束时,变量引用消失,无法再访问该变量。 你可以认为变量decalred与student变量不同:student.totalAssignments总是等于零,因为没有任何值对他有影响。

然后假设你可以这样做:

student1.totalAssignments  = input.nextInt();
student1.totalPoints  = input.nextDouble();