调用对象方法未得到预期的结果

时间:2019-02-01 22:05:25

标签: java

我有两个.java文件,一个是“蓝图”,另一个仅包含main方法。我遇到的问题是,我想打印与输入的数字相对应的字母。我可以对一个“ student”对象正常工作,但是当创建另一个“ student2”对象时,无论用户输入多少数字,字母“ F”都会每次打印。请查看下面的代码以更好地理解。

public class StudentRecord


public String firstName;
public String lastName;
public int studentID;
public int courseGradeOne;


//create a constructor with no args.
public StudentRecord()
{

}//end of constructor without args.

public StudentRecord(String firstName, String lastName, int studentID, int courseGradeOne)
{
    //initializing variables.
    this.firstName = firstName;
    this.lastName = lastName;
    this.studentID = studentID;
    this.courseGradeOne = courseGradeOne;
}//end of constructor with args.



/*
Setters & Getters for all 'fields'.
*/


public void setFirstName(String firstName)
{
    firstName = firstName;

}//end of setter firstName.

public String getFirstName()
{
    return firstName;
}//end of getter firstName.

public void setLastName(String lastName)
{
    lastName = lastName;

}//end of setter lastName.

public String getLastName()
{
    return lastName;
}//end of getter lastName.

public void setStudentID(int studentID)
{
    studentID = studentID;

}//end of setter studentID.

public int getStudentID()
{
    return studentID;
}//end of getStudentID.

public void setCourseGradeOne(int courseGradeOne)
{
    courseGradeOne = courseGradeOne;

}//end of setter courseGradeOne.

public int getCourseGradeOne()
{
    return courseGradeOne;
}//end of getter courseGradeOne.


/*
Decision statements to determine "letter grade".
*/

public char letterReturn()
{
    if(courseGradeOne >= 90)
        return 'A';
    else
        if(courseGradeOne >= 80)
            return 'B';
        else
            if(courseGradeOne >= 70)
                return 'C';
            else
                if(courseGradeOne >= 60)
                    return 'D';
                else
                    if(courseGradeOne >= 50)
                        return 'E';
                    else
                        return 'F';
}//end of letterReturn.

SECOND .java文件

公共类TestStudentRecord {

static Scanner input = new Scanner(System.in);

public static void main(String[] args)
{
    //creating student1 'object'.
    StudentRecord student1 = new StudentRecord("Bob", "Smith", 111111, 100);

    //print information about student1.
    System.out.println("First Name: " + student1.getFirstName());
    System.out.println("Last Name: " + student1.getLastName());
    System.out.println("Student Number: " + student1.getStudentID());
    System.out.println("Course Grade(letter): " + student1.letterReturn());

    //blank prints to improve readability.
    System.out.println("");
    System.out.println("");
    System.out.println("");
    //blank prints to improve readability.

    //creating student2 'object'.
    StudentRecord student2 = new StudentRecord();

    //ask information about student2.
    System.out.println("What is your first name?");
    String firstName = input.nextLine();

    System.out.println("What is your last name?");
    String lastName = input.nextLine();

    System.out.println("What is your student number?");
    int studentID = input.nextInt();

    System.out.println("What is your grade in the course?");
    int courseGradeOne = input.nextInt();

    //print supplied information to user.
    System.out.println("First Name: " + firstName);
    System.out.println("Last Name: " + lastName);
    System.out.println("Student Number: " + studentID);
    System.out.println("Course Grade(letter): " + student2.letterReturn());

您可以看到,最后一行是让我绊倒的东西,无论第二名学生输入什么,它总是显示“ F”。也许我发布了太多不必要的代码,但希望您能理解这一点,而不是我的la脚技能试图解释我在做什么。任何反馈表示赞赏。

1 个答案:

答案 0 :(得分:0)

利用吸气/ setter方法将有助于在STUDENT2的情况下

//creating student2 'object'.
StudentRecord student2 = new StudentRecord();

//ask information about student2.
System.out.println("What is your first name?");
String firstName = input.nextLine();
student2.setFirstName(firstName);

//print supplied information to user.
System.out.println("First Name: " + student2.getFirstName());

做同样的事情,其他的,它应该工作了。