gpt4 book ai didi

java - 我试图避免彩票行中出现重复,即如果数字 35 位于第一彩票行中,那么它一定不能位于第二行中

转载 作者:行者123 更新时间:2023-11-30 05:26:59 24 4
gpt4 key购买 nike

我试图避免彩票行中出现重复,即如果数字 35 位于第一彩票行中,那么它一定不能位于第二行中我很困惑为什么我不断收到重复的内容我添加了一张可以更好地解释我的问题的图像?解决这个问题的最佳方法是什么?我尝试将 nt lottoTal = rand.nextInt(35) + 1 更改为 nt lottoTal = rand.nextInt(34) + 1

public class LottoCanvas extends BorderPane{

private Canvas canvas;
private GraphicsContext gc;
private Text text;
private Text textExtra; ///
private Set<Number> lottorad;
private Set<Number> lottoradExtra;//
Random rand;
public LottoCanvas() { //constructor

lottorad = new HashSet();
lottoradExtra = new HashSet();
rand = new Random();
canvas = new Canvas(250, 250);
gc = canvas.getGraphicsContext2D();

Button button = new Button("Rita om");
button.setOnAction(new EventHandler() {

@Override
public void handle(Event event) {
drawLotto(gc);

}

});//end EventHandler

VBox vbox = new VBox();
vbox.setPadding(new Insets(5,5,5,5));
vbox.setAlignment(Pos.CENTER);
text = new Text();
textExtra = new Text(); //
vbox.getChildren().addAll(text,textExtra,button);
drawLotto(gc);
setCenter(canvas);
setBottom(vbox);

}//end constructor
private void createLottoRad(){

lottorad.clear();
lottoradExtra.clear();


while (lottorad.size()<7) {
int lottoTal = rand.nextInt(35) + 1;
lottorad.add(lottoTal);
}
while (lottoradExtra.size()<4) {
int lottoTal = rand.nextInt(35) + 1;
lottoradExtra.add(lottoTal);
}



}//end metoden createLottoRad

private void drawLotto(GraphicsContext gc) {

createLottoRad();

int x = 0;
int y = 0;
int bredd=30;
int hojd=30;
for (int i = 0; i < 35; i++) {
x += 35;
if (i % 7 == 0){
y += 35;
x = 0;
}
if (lottorad.contains(i+1)){
gc.setFill(Color.RED);
}

if (lottoradExtra.contains(i+1)){
gc.setFill(Color.GREEN);
}

gc.fillRoundRect(x, y, bredd, hojd , 10, 10);
gc.setFill(Color.WHITE);

gc.fillText("" + (i + 1), x + 10, y + 20);
gc.setFill(Color.BLACK);
}

List myList = new ArrayList(lottorad);
Collections.sort(myList);
text.setText(myList.toString());

List myListExtra = new ArrayList(lottoradExtra);
Collections.sort(myListExtra);
textExtra.setText(myListExtra.toString());

}//end metoden drawLotto

}//enter piture of what i am trying to avoid

最佳答案

I am trying to avoid duplicates in my lottery rows , ie if number 35 is in the first lottery row then it must not be in the second row I am confused about why I keep on getting duplicates I have added a image that explains my problem better? What is the best way to go about this probelem?

您当前的方法是这样的:

  • 将随机数字添加到 lottorad 中,直到其中出现 7 个唯一值。
  • 将随机数添加到 lottoradExtra 中,直到其中有 4 个唯一值。

这两个集合是独立的,因此没有什么可以阻止两个集合中具有相同的数字。您可以在填充 lottoradExtra 时添加显式条件,以跳过该号码(如果该号码已在 lottorad 中),例如:

while (lottoradExtra.size() < 4) {
int lottoTal = rand.nextInt(35) + 1;
if (lottorad.contains(lottoTal)) {
continue;
}
lottoradExtra.add(lottoTal);
}

但是,当前的方法是有缺陷的,因为当相同的数字不断重复出现时,执行额外的循环有点浪费。理论上,您的程序可能不会终止。

另一种方法是构建一个数字列表,将其打乱,然后只取前 7 个值,然后取前 4 个值。这样就保证不会出现重复的情况。例如:

List<Integer> numbers = IntStream.rangeClosed(1, 35).boxed().collect(Collectors.toList());
Collections.shuffle(numbers);

lottorad.clear();
lottorad.addAll(numbers.subList(0, 7));

lottoradExtra.clear();
lottoradExtra.addAll(numbers.subList(7, 11));

关于java - 我试图避免彩票行中出现重复,即如果数字 35 位于第一彩票行中,那么它一定不能位于第二行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357762/

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