我有一个枚举类型 Teams,我想要将其随机化。所以我有以下代码:
public enum Teams { TEAM1, TEAM2, TEAM3, TEAM4, TEAM5, TEAM6; }
我有一个随机方法来随机生成值:
public static Teams getRandomTeam() {
return Teams.values()[(int) (Math.random() * Teams.values().length)];
}
这段代码可以返回一个随机生成的团队,但是我需要做到一旦生成了一个名为TEAM2的团队,就不能再次生成。
我正在使用:
System.out.println("The team is " + getRandomTeam());
System.out.println("The team is " + getRandomTeam());
System.out.println("The team is " + getRandomTeam());
System.out.println("The team is " + getRandomTeam());
System.out.println("The team is " + getRandomTeam());
System.out.println("The team is " + getRandomTeam());
我知道目前的做法是错误的,因为它会一遍又一遍地调用这个方法。
目前当我运行程序时,输出结果可能是:
队伍是:TEAM2
队伍是:TEAM2
队伍是:TEAM4
队伍是:TEAM2
队伍是:TEAM3
队伍是:TEAM2
但我需要我的程序只输出一次枚举值。
谢谢。