class Test {
public static void main(String args[]) {
Tournament T = new Tournament();
Team t = new Team();
T.getInfo2();
/*T.display2();
T.teamObject();*/
int teams = T.num_team;
int players;
Team[] tArr = new Team[teams];
for (int i = 0; i < teams; i++) {
tArr[i] = new Team();
tArr[i].getInfo1();
players = t.num_players;
player[] pArr = new player[players];
for (int j = 0; j < players; j++) {
pArr[j] = new player();
pArr[j].getInfo();
}
}
}
为什么第二个for循环没有运行? 没有编译时错误或运行时错误。 我无法解决问题。
答案 0 :(得分:2)
第二个循环正在按预期执行。可能t.num_players
为零,即被分配给玩家。
答案 1 :(得分:0)
以这种方式尝试:
for (int i = 0; i < teams; i++) {
tArr[i] = new Team();
tArr[i].getInfo1();
players = tArr[i].num_players;
// this way you will loop over the num_players of the new team you created
// MAYBE this is the problem
player[] pArr = new player[players];
for (int j = 0; j < players; j++) {
pArr[j] = new player();
pArr[j].getInfo();
}
}
并且请阅读Java代码约定,不遵守camelCase编码风格使得读取代码非常混乱,类应该以大写字母开头,每个单词没有空格,变量以小写字母开头,第一个单词,然后每个其他单词以大写字母开头,没有空格....例如
Object object = new Object();
MyClass myClass = new MyClass();
MyArray myArray = new MyArray[myClass.getArrayLength()];
等等,在名为T的java中没有变量,甚至不希望类被命名为,因为已知T代表泛型中的类型,类似于E代表元素
很抱歉谈论一个超出范围的主题,但是我阅读代码的问题有50%是因为这样简单的规则