gpt4 book ai didi

java - 如何防止随机匹配生成器重复?

转载 作者:行者123 更新时间:2023-12-01 22:43:33 25 4
gpt4 key购买 nike

我制作了一个随机摔跤比赛生成器,它是根据教科书中的随机短语生成器改编的。我想知道如何制作,这样同一个名字就不会在同一次运行中出现两次。不能让 splinter 机对 splinter 机,对吗?

public class matchOMatic {
public static void main (String [] args) {

String [] wordListOne = {"The Crusher", "The Main Man", "The Macho-man, Randy Savage", "The Nature Boy, Rick Flare", "Batista", "Hollywood Hulk Hogan", "Vader", "The Undertaker", "Stone Cold Steve Austin" };
String [] wordListThree = {"The Crusher", "The Main Man", "The Macho-man, Randy Savage", "The Nature Boy, Rick Flare", "Batista", "Hollywood Hulk Hogan", "Vader", "The Undertaker", "Stone Cold Steve Austin"};

int oneLength = wordListOne.length;
int threeLength = wordListThree.length;

int rand1 = (int) (Math.random() * oneLength);
int rand3 = (int) (Math.random() * threeLength);

String phrase = wordListOne[rand1] + " and in the opposite corner is his opponent, " + wordListThree[rand3];

System.out.print("In this corner we have " + phrase);
System.out.println("!");
}
}

最佳答案

非常简单的解决方案!

只需将其放在为 rand3 和 rand2 赋值之后即可:

while(rand3 == rand1) {
rand3 = (int) (Math.random() * threeLength);
}

这将为 rand3 不断选择新值,直到值不同为止!

我希望这有帮助。祝你的程序好运:)

关于java - 如何防止随机匹配生成器重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25716179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com