为什么学生计数器跳过一个数字
Why is the student counter skipping a number
我正在尝试构建一个程序,从 1 到 10 对每个学生进行计数。但输出似乎在 4 之后跳过 5,直接进入 6。
我得到的输出是:
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
主要class:
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();
}
}
学生class:
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();
}
}
ComputerStudent class:
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 + ": ");
}
}
MathStudent class:
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 + ": ");
}
}
理科学生class:
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 + ": ");
}
}
StudentThread class:
public class StudentThread extends Thread {
public void run(){
Student s[] = new Student[10];
s[0] = new MathStudent("Smith");
s[1] = new MathStudent("Jack");
s[2] = new MathStudent("Victor");
s[3] = new MathStudent("Mike");
s[4] = new ScienceStudent("Dave");
s[5] = new ScienceStudent("Oscar");
s[6] = new ScienceStudent("Peter");
s[7] = new ComputerStudent("Philip");
s[8] = new ComputerStudent("Shaun");
s[9] = new ComputerStudent("Scott");
for (int j = 0; j < 10; j++) {
for (Student item : s) {
System.out.print(item.getSubjects() + " - " + "Count:");
item.getCounter();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
}
}
我的问题是为什么输出会跳过 5?
在科学领域 Class:
super(name);
studentCounter++;
studCountObj=studentCounter;
您首先将 studentCounter
从 5 增加到 6,然后将其分配给 studCountObj
。交换将打印 5.
你必须改变这个:
数学学生 class:
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 + ": ");
}
}
还有这个:
学生class:
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 class 和 Computer 学生 class 中,您已经这样做了,因此当您在 Mathstudent class 中更改此模式时,您将获得第一个输出,因为当线程转到 ScienceStudent 时它会再次添加数字 1,然后你跳过一个数字。
我正在尝试构建一个程序,从 1 到 10 对每个学生进行计数。但输出似乎在 4 之后跳过 5,直接进入 6。 我得到的输出是:
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
主要class:
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();
}
}
学生class:
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();
}
}
ComputerStudent class:
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 + ": ");
}
}
MathStudent class:
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 + ": ");
}
}
理科学生class:
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 + ": ");
}
}
StudentThread class:
public class StudentThread extends Thread {
public void run(){
Student s[] = new Student[10];
s[0] = new MathStudent("Smith");
s[1] = new MathStudent("Jack");
s[2] = new MathStudent("Victor");
s[3] = new MathStudent("Mike");
s[4] = new ScienceStudent("Dave");
s[5] = new ScienceStudent("Oscar");
s[6] = new ScienceStudent("Peter");
s[7] = new ComputerStudent("Philip");
s[8] = new ComputerStudent("Shaun");
s[9] = new ComputerStudent("Scott");
for (int j = 0; j < 10; j++) {
for (Student item : s) {
System.out.print(item.getSubjects() + " - " + "Count:");
item.getCounter();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
}
}
我的问题是为什么输出会跳过 5?
在科学领域 Class:
super(name);
studentCounter++;
studCountObj=studentCounter;
您首先将 studentCounter
从 5 增加到 6,然后将其分配给 studCountObj
。交换将打印 5.
你必须改变这个: 数学学生 class:
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 + ": ");
}
}
还有这个: 学生class:
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 class 和 Computer 学生 class 中,您已经这样做了,因此当您在 Mathstudent class 中更改此模式时,您将获得第一个输出,因为当线程转到 ScienceStudent 时它会再次添加数字 1,然后你跳过一个数字。