我有一个要随机化的枚举团队。所以我有:
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
但是我需要我的程序只输出一次枚举值。谢谢