为什么学生会跳过一个号码

时间:2017-03-18 15:56:49

标签: java arrays multithreading count counter

我正在尝试建立一个程序,从1到10计算每个学生。但输出似乎在4之后跳过5并直接到6。 我得到的输出是:

<html>
  <head><title>Page not found :o</title></head>
  <body>
    <h1>Sorry, we can't find what you're looking for.</h1>
    <a href="https://example.com">Take me back to the main site.</a>
  </body>
</html>

主要课程:

MathStudent[1]-Smith:  - Count:1
 MathStudent[2]-Jack:  - Count:1
 MathStudent[3]-Victor:  - Count:1
 MathStudent[4]-Mike:  - Count:1
 Science Student[6]-Dave:  - Count:1
 Science Student[7]-Oscar:  - Count:1
 Science Student[8]-Peter:  - Count:1
 Computer Student[9] Philip:  - Count:1
 Computer Student[10] Shaun:  - Count:1
 Computer Student[11] Scott:  - Count:1

学生班:

public class JavaLab5 {
    public static final int DEBUG = 0;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        StudentThread studentThread = new StudentThread();
        studentThread.start();


    }

}

ComputerStudent类:

public class Student {
    static int  studentCounter = 1;
    String name;
    private int count = 0;
    public static int instances = 0;

    // Getters
    public  String getName() {
        return this.name;
    }

    // Setters
    public void setName(String name) {
        if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
        this.name = name;
    }

    /**
     * Default constructor. Populates name,age and gender 
     * with defaults
     */
    public Student() {
        instances++;
        this.name = "Not Set";
    }

    /** 
     * Constructor with parameters 
     * @param name String with the name
    */
    public Student(String name) {
        this.name = name;
    }

    /**
     * Destructor
     * @throws Throwable 
     */
    protected void finalize() throws Throwable {
        //do finalization here
        instances--;
        super.finalize(); //not necessary if extending Object.
    } 

    /**
     *
     */
    public void getCounter() {
        for (int j = 0; j < 1; j++) {
        count++;
        System.out.println(count);
    }
    }
    @Override
    public String toString () {
        return this.name; 
    }

    public String getSubjects() {
      return this.getSubjects();
    }


}

MathStudent课程:

public class ComputerStudent extends Student {
    int studCountObj;


    /**
     * Default constructor
     * @fortanGrade
     * @adaGrade
     */
    public ComputerStudent() {
        super();
    }

    public ComputerStudent(String name) {
        super(name);
        studentCounter++;
        studCountObj=studentCounter;

    }


    /**
     * Display information about the subject
     * @return 
     */
    @Override
    public String getSubjects(){
        return(" Computer Student" + "[" + studCountObj + "] " + name + ": "); 
    }
}

ScienceStudent课程:

public class MathStudent extends Student {
    int studCountObj;

    /**
     * Default constructor
     * @param name
     */
    public MathStudent(String name) {
        super(name);
        studCountObj=studentCounter;
        studentCounter++;
    }

    public MathStudent() {
        super();
    }

    /**
     * Display information about the subject
     * @return 
     */
    @Override
    public String getSubjects(){
        return(" MathStudent" + "[" + studCountObj + "]" + "-" + name + ": ");
    }
}

StudentThread课程:

public class ScienceStudent extends Student {
    int studCountObj;
    /**
     * Default constructor
     */
    public ScienceStudent() {
        super();
    }

    public ScienceStudent(String name) {
        super(name);
        studentCounter++;
        studCountObj=studentCounter;
    }

    /**
     * Display information about the subject
     * @return 
     */
     @Override
    public String getSubjects(){
        return(" Science Student" + "[" + studCountObj + "]" + "-" + name + ": ");
    } 
}

我的问题是为什么输出会跳过5?

2 个答案:

答案 0 :(得分:0)

在科学课上:

super(name);
studentCounter++;
studCountObj=studentCounter;

您首先将studentCounter从5增加到6,然后将其分配给studCountObj。交换将打印5。

答案 1 :(得分:0)

你必须改变这个: MathStudent课程:

public class MathStudent extends Student {
int studCountObj;

/**
 * Default constructor
 * @param name
 */
public MathStudent(String name) {
    super(name);
    studentCounter++;      //First this
    studCountObj=studentCounter;  //Then this

}

public MathStudent() {
    super();
}

/**
 * Display information about the subject
 * @return 
 */
@Override
public String getSubjects(){
    return(" MathStudent" + "[" + studCountObj + "]" + "-" + name + ": ");
}

}

这个: 学生班:

public class Student {
static int  studentCounter = 0;      //Start with zero
String name;
private int count = 0;
public static int instances = 0;

// Getters
public  String getName() {
    return this.name;
}

// Setters
public void setName(String name) {
    if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
    this.name = name;
}

/**
 * Default constructor. Populates name,age and gender 
 * with defaults
 */
public Student() {
    instances++;
    this.name = "Not Set";
}

/** 
 * Constructor with parameters 
 * @param name String with the name
*/
public Student(String name) {
    this.name = name;
}

/**
 * Destructor
 * @throws Throwable 
 */
protected void finalize() throws Throwable {
    //do finalization here
    instances--;
    super.finalize(); //not necessary if extending Object.
} 

/**
 *
 */
public void getCounter() {
    for (int j = 0; j < 1; j++) {
    count++;
    System.out.println(count);
}
}
@Override
public String toString () {
    return this.name; 
}

public String getSubjects() {
  return this.getSubjects();
}

}

在ScienceStudent课程和计算机学生课程中,你已经这样做了,所以当你在Mathstudent课程中更改这个模式时,你将得到你的第一个输出,因为当线程进入ScienceStudent时它会再次添加数字1,你跳过一个数字。