宝贝在java中的第一个类和方法

时间:2018-02-12 21:37:51

标签: java

好的,所以我的第一个任务就是我对Java类的介绍。我已经阅读了本书的前两章,现在可能已经阅读了6次,我在网上查看了十几个不同的教程,但我仍然没有得到问题所在。

我有班级学生

//declare main class
public class Student {
// declare variables needed including ID_number, Credit_hours, points and GPA
private float ID_number;
private int Credit_hours;
private int points;
public float GPA;
// methods will go here
//method to return ID number
public float getID_number()
{
    return ID_number;
}
//method to set the ID number
public void setID_number(float ID_number)
{
    ID_number = ID_number;
}
//method to return credit hours
public int getCredit_hours()
{
    return Credit_hours;
}
//method to set credit hours
public void setCredit_hours(int Creds)
{
    Credit_hours = Creds;
}
//method to get points
public int getpoints()
{
    return points;
}
//method to set points
public void setpoints(int points)
{
    points = points;
}
//method to calculate and return GPA
public float setGPA()
{
    GPA = points/Credit_hours; 
    //return GPA;

}
//method to print the GPA
public float getGPA(float GPA)
{
    return GPA;

}
}

然后我有了ShowStudent类,它应该实例化Student Class:

import java.util.Scanner;
class ShowStudent
{
public static void main (String args[])
{
Student newStudent;
newStudent = getStudentInfo();
displayStudent(newStudent);
}
public static Student getStudentInfo()
{
    Student tempStu = new Student();
    float ID_number;
    int Credit_hours;
    int points;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Student ID Number >> ");
    ID_number = input.nextFloat();
    tempStu.setID_number(ID_number);


}   
public static void displayStudent(Student aStu)
   {
       System.out.println("\nStudnet number is:  #" + aStu.getID_number() +
          " While their GPA is " + aStu.getGPA());
   }
}

但是eclipse正在向我抛出各种错误,包括以下内容:

“方法必须返回类型学生”showStudent中的第12行

“方法getGPA不适用于此参数”showStudent中的第28行

我只是不明白这里的问题是什么,我对此感到非常沮丧。

1 个答案:

答案 0 :(得分:1)

首先,您getStudentInfo()必须返回Student这样的对象

public static Student getStudentInfo()
{
    Student tempStu = new Student();
    float ID_number;
    int Credit_hours;
    int points;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Student ID Number >> ");
    ID_number = input.nextFloat();
    tempStu.setID_number(ID_number);

return tempStu;
}

其次,你在getters and setters课程中混淆了student和一些变量实现。它应该是这样的

public class Student {
// declare variables needed including ID_number, Credit_hours, points and GPA
private float ID_number;
private int Credit_hours;
private int points;
public float GPA;
// methods will go here
//method to return ID number
public float getID_number()
{
    return ID_number;
}
//method to set the ID number
public void setID_number(float ID_number)
{
    this.ID_number = ID_number;
}
//method to return credit hours
public int getCredit_hours()
{
    return Credit_hours;
}
//method to set credit hours
public void setCredit_hours(int Creds)
{
    Credit_hours = Creds;
}
//method to get points
public int getpoints()
{
    return points;
}
//method to set points
public void setpoints(int points)
{
    this.points = points;
}
//method to calculate and return GPA
public float getGPA()
{
    return GPA;

}
//method to print the GPA
public void setGPA(float GPA)
{

    this.GPA = GPA; 

}
}