我正在尝试创建一个程序,它将提供以下输出:
MathStudent[1] Jack Count: 1
MathStudent[2] Smith Count: 1
MathStudent[3] Victor Count: 1
MathStudent[4] Mike Count: 1
ScienceStudent[5] Dave Count: 1
ScienceStudent[6] Oscar Count: 1
ScienceStudent[7] Peter Count: 1
ComputerStudent[8] Philip Count 1
ComputerStudent[9] Shaun Count 1
ComputerStudent[10] Scott Count 1
MathStudent[1] Jack Count: 2
MathStudent[2] Smith Count: 2
MathStudent[3] Victor Count: 2
MathStudent[4] Mike Count: 2
ScienceStudent[5] Dave Count: 2
ScienceStudent[6] Oscar Count: 2
ScienceStudent[7] Peter Count: 2
ComputerStudent[8] Philip Count 2
ComputerStudent[9] Shaun Count 2
ComputerStudent[10] Scott Count 2
:
:
:
MathStudent[1] Jack Count: 10
MathStudent[2] Smith Count: 10
MathStudent[3] Victor Count: 10
MathStudent[4] Mike Count: 10
ScienceStudent[5] Dave Count: 10
ScienceStudent[6] Oscar Count: 10
ScienceStudent[7] Peter Count: 10
ComputerStudent[8] Philip Count 10
ComputerStudent[9] Shaun Count 10
ComputerStudent[10] Scott Count 10
但我得到的输出是:
MathStudent[1]-Smith: - Count:1
MathStudent[2]-Jack: - Count:1
MathStudent[3]-Victor: - Count:1
MathStudent[4]-Mike: - Count:1
Science Student[5]-Dave: - Count:1
Science Student[6]-Oscar: - Count:1
Science Student[7]-Peter: - Count:1
Computer Student[8] Philip: - Count:1
Computer Student[9] Shaun: - Count:1
Computer Student[10] Scott: - Count:1
MathStudent[11]-Smith: - Count:1
MathStudent[12]-Jack: - Count:1
MathStudent[13]-Victor: - Count:1
MathStudent[14]-Mike: - Count:1
Science Student[15]-Dave: - Count:1
Science Student[16]-Oscar: - Count:1
Science Student[17]-Peter: - Count:1
Computer Student[18] Philip: - Count:1
Computer Student[19] Shaun: - Count:1
Computer Student[20] Scott: - Count:1
MathStudent[21]-Smith: - Count:1
MathStudent[22]-Jack: - Count:1
MathStudent[23]-Victor: - Count:1
MathStudent[24]-Mike: - Count:1
Science Student[25]-Dave: - Count:1
Science Student[26]-Oscar: - Count:1
Science Student[27]-Peter: - Count:1
Computer Student[28] Philip: - Count:1
Computer Student[29] Shaun: - Count:1
Computer Student[30] Scott: - Count:1
MathStudent[31]-Smith: - Count:1
MathStudent[32]-Jack: - Count:1
MathStudent[33]-Victor: - Count:1
MathStudent[34]-Mike: - Count:1
Science Student[35]-Dave: - Count:1
Science Student[36]-Oscar: - Count:1
Science Student[37]-Peter: - Count:1
Computer Student[38] Philip: - Count:1
Computer Student[39] Shaun: - Count:1
Computer Student[40] Scott: - Count:1
MathStudent[41]-Smith: - Count:1
MathStudent[42]-Jack: - Count:1
MathStudent[43]-Victor: - Count:1
MathStudent[44]-Mike: - Count:1
Science Student[45]-Dave: - Count:1
Science Student[46]-Oscar: - Count:1
Science Student[47]-Peter: - Count:1
Computer Student[48] Philip: - Count:1
Computer Student[49] Shaun: - Count:1
Computer Student[50] Scott: - Count:1
MathStudent[51]-Smith: - Count:1
MathStudent[52]-Jack: - Count:1
MathStudent[53]-Victor: - Count:1
MathStudent[54]-Mike: - Count:1
Science Student[55]-Dave: - Count:1
Science Student[56]-Oscar: - Count:1
Science Student[57]-Peter: - Count:1
Computer Student[58] Philip: - Count:1
Computer Student[59] Shaun: - Count:1
Computer Student[60] Scott: - Count:1
MathStudent[61]-Smith: - Count:1
MathStudent[62]-Jack: - Count:1
MathStudent[63]-Victor: - Count:1
MathStudent[64]-Mike: - Count:1
Science Student[65]-Dave: - Count:1
Science Student[66]-Oscar: - Count:1
Science Student[67]-Peter: - Count:1
Computer Student[68] Philip: - Count:1
Computer Student[69] Shaun: - Count:1
Computer Student[70] Scott: - Count:1
MathStudent[71]-Smith: - Count:1
MathStudent[72]-Jack: - Count:1
MathStudent[73]-Victor: - Count:1
MathStudent[74]-Mike: - Count:1
Science Student[75]-Dave: - Count:1
Science Student[76]-Oscar: - Count:1
Science Student[77]-Peter: - Count:1
Computer Student[78] Philip: - Count:1
Computer Student[79] Shaun: - Count:1
Computer Student[80] Scott: - Count:1
MathStudent[81]-Smith: - Count:1
MathStudent[82]-Jack: - Count:1
MathStudent[83]-Victor: - Count:1
MathStudent[84]-Mike: - Count:1
Science Student[85]-Dave: - Count:1
Science Student[86]-Oscar: - Count:1
Science Student[87]-Peter: - Count:1
Computer Student[88] Philip: - Count:1
Computer Student[89] Shaun: - Count:1
Computer Student[90] Scott: - Count:1
MathStudent[91]-Smith: - Count:1
MathStudent[92]-Jack: - Count:1
MathStudent[93]-Victor: - Count:1
MathStudent[94]-Mike: - Count:1
Science Student[95]-Dave: - Count:1
Science Student[96]-Oscar: - Count:1
Science Student[97]-Peter: - Count:1
Computer Student[98] Philip: - Count:1
Computer Student[99] Shaun: - Count:1
Computer Student[100] Scott: - Count:1
主要课程是:
public class JavaLab5 {
public static final int DEBUG = 0;
public static void multipleThreads(int number) {
StudentThread []st = new StudentThread[number];
for (int j = 0; j < number; j++) {
st[j] = new StudentThread();
st[j].start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Exception at PrintThread.run: " + e);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//StudentThread studentThread = new StudentThread();
//studentThread.start();
multipleThreads(10);
}
}
学生班:
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();
}
}
电脑学生:
public class ComputerStudent extends Student {
int studCountObj;
/**
* Default constructor
*/
public ComputerStudent() {
super();
}
public ComputerStudent(String name) {
super(name);
studCountObj=studentCounter;
studentCounter++;
}
/**
* Display information about the subject
* @return
*/
@Override
public String getSubjects(){
return(" Computer Student" + "[" + studCountObj + "] " + name + ": ");
}
}
数学学生:
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 + ": ");
}
}
理科学生:
public class ScienceStudent extends Student {
int studCountObj;
/**
* Default constructor
*/
public ScienceStudent() {
super();
}
public ScienceStudent(String name) {
super(name);
studCountObj=studentCounter;
studentCounter++;
}
/**
* Display information about the subject
* @return
*/
@Override
public String getSubjects(){
return(" Science Student" + "[" + studCountObj + "]" + "-" + name + ": ");
}
}
StudentThread课程: public class StudentThread扩展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 (Student item : s) {
System.out.print(item.getSubjects() + " - " + "Count:");
item.getCounter();
}
}
}
我有两个问题,一个是学生编号一直到100,但它应该只有10个,然后10个学生应该重复,而括号内的学生编号不会增加。
问题是,计数没有上升,只是停留在1。
答案 0 :(得分:0)
学生号码一直到100,但它应该只去 最多10个,然后10个学生应该重复,没有 括号内的学号增加。
要解决此问题,您只需要创建10个学生,但您目前正在创建100个学生。您可以通过传递multipleThreads(1);
或删除循环for (int j = 0; j < number; j++)
第二个问题是计数不会上升它只是 住在1。
您的count
变量不是static
所以它将为每个Student
对象创建,因此要解决此问题,请将其标记为静态变量,即private static int count = 0;
内部Student
上课。