java - Baby 的第一个 Java 类和方法

标签 java

好的,现在正在完成我的第一个 Java 类简介作业。我已经把这本书的前两章读了大约 6 遍,并且在网上查看了十几个不同的教程,但我仍然不明白这里的问题是什么。

我有类(class)学生

//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 类:

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 行

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

最佳答案

首先,您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;
}

其次,您错误地使用了 getter 和 setter 以及 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; 

}
}

关于java - Baby 的第一个 Java 类和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48755915/

相关文章:

java - 如果我自己调用 run() 方法会发生什么?

java - 循环序列停止

java - 如何在 JSP 中以多种不同方式对列表进行排序?

java - android-透明的RelativeLayout

java - Eclipse Preferences 不保存类路径变量

java - 我是否必须检查 String 对象是否为空?

java - 无法计算 9/30/11 2 :58:49 PM EST? 的日期格式字符串

java - 有条件地忽略 Jackson 的原始类型字段

java - 从 RGBA 到十六进制

Java 意外追加