我创建了两个类(Team和MarchMadness)来在Java中创建MarchMadness括号。在MarchMadness
类中,我为每个回合创建了一个ArrayList
类型的Team
,并添加了round1的所有团队。但是,尝试在randomize
方法中拉ArrayList的大小和元素时,我总是收到“无法解析”错误。
我尝试将ArrayList的方法移到main
方法之外,但是当我尝试将团队添加到ArrayList时出现错误。我也在网上搜索,但没有找到任何内容。
这是我的代码,只有东部第一回合的球队。
public class Team extends MarchMadness {
private String name;
private double rating;
private int seed;
public Team(String name, double rating, int seed) {
this.name = name;
this.rating = rating;
this.seed = seed;
}
}
import java.util.ArrayList;
import java.util.List;
public class MarchMadness {
public static void main(String[] args) {
ArrayList<Team> round1East = new ArrayList<Team>();
round1East.add(new Team("Duke", 0.670, 1));
round1East.add(new Team("North Dakota St", 0.495, 16));
round1East.add(new Team("VCU", 0.609, 8));
round1East.add(new Team("UCF", 0.606, 9));
round1East.add(new Team("Mississippi St", 0.609, 5));
round1East.add(new Team("Liberty", 0.549, 12));
round1East.add(new Team("Virginia Tech", 0.605, 4));
round1East.add(new Team("Saint Louis", 0.547, 13));
round1East.add(new Team("Maryland", 0.602, 6));
round1East.add(new Team("Belmont", 0.579, 11));
round1East.add(new Team("LSU", 0.626, 3));
round1East.add(new Team("Yale", 0.572, 14));
round1East.add(new Team("Louisville", 0.594, 7));
round1East.add(new Team("Minnesota", 0.578, 10));
round1East.add(new Team("Michigan St", 0.641, 2));
round1East.add(new Team("Bradley", 0.504, 15));
}
private void randomize() {
if(round2East.isEmpty()) {
for(int i = 0; i < round1East.size(); i += 2) {
int x = (int)(Math.random() * 2);
if(x == 0) {
round2East.add(round1East.get(i));
} else {
round2East.add(round1East.get(i++));
}
}
}
}
答案 0 :(得分:0)
这是因为当您将ArrayList放在main方法之外时,您将其声明为“实例变量”。由于主要方法是静态的,因此您不能在静态方法中引用“非静态(实例)”变量。
解决方法是只在main方法之外声明它,但也将变量设为静态。
static ArrayList<Team> round1East = new ArrayList<Team>();
答案 1 :(得分:0)
您还必须尝试使用List Obj = new ArrayList,这使您可以轻松地在List接口的不同实现之间进行切换。多态性