学生班和对象

时间:2014-11-15 04:53:21

标签: java object

我正在尝试编写一个带有学生姓名的程序,并打印出学生的考试成绩及其姓名。到目前为止,该程序显示null为学生姓名和" 0"为考试成绩。 我已经为我正在使用的变量赋值,但它似乎只是跳过它。这是代码:

package studentTestScores;
public class Student {

  public static void main(String[] args) 
    {
    Student s1, s2;
    String str;
    int i;

    s1 = new Student();

    s1.setName("Bill");
    s1.setScore(1, 84);
    s1.setScore(2, 86);
    s1.setScore(3, 88);

    str = s1.getName();
    System.out.println(str);

    i = s1.getHighScore();
    System.out.println(i);

   }

   public void setScore(int i, int j) {
     // TODO Auto-generated method stub
   }

   public int getHighScore() {
     // TODO Auto-generated method stub
     return 0;
   }

   public String getName() {
     // TODO Auto-generated method stub
     return null;
   }

   public void setName(String string) {
     // TODO Auto-generated method stub
   }
}

1 个答案:

答案 0 :(得分:-2)

希望这就是你需要的程序。

package studentTestScores;
public class Student {

public int highScore;
public string studentName;
public int subjectNum;

public static void main(String[] args) 
{
    Student s1;
    String str;
    int i;

    s1 = new Student();

    s1.highScore = 0;
    s1.studentName = "";
    s1.subjectNum = 0;

    s1.setName("Bill");
    s1.setScore(1, 84);
    s1.setScore(2, 86);
    s1.setScore(3, 88);

    str = s1.getName();
    System.out.println(str);

    i = s1.getHighScore();
    System.out.println(i);

}

 public void compareAndSetHighScore(int i, int j) {
     if(highScore < j)
     {
        highScore = j;
        subjectNum = i;//If you need to print subject Number too.
     }
 }

 public int getHighScore() {
     return highScore;
 }

 public String getName() {
     return studentName ;
 }

 public void setName(String string) {
     studentName = string;
 }

}