【问题标题】:Is it possible to do a shuffle in this manner with a boolean instead of a list?是否可以使用布尔值而不是列表以这种方式进行洗牌?
【发布时间】:2022-01-20 12:07:59
【问题描述】:

这里我的代码(在 Eclipse 上的 Java 中)显示来自文件的 3 张随机卡片。我试图让一个洗牌按钮工作并随机洗牌 3 张新牌。我使用了“Collections.shuffle(cards);”并将我的布尔数组传递给它,但它说我不能,因为它想要一个 List 列表。是否可以让随机播放与我的布尔值一起使用,还是我必须使用列表?

这是我的代码:

import java.util.Collections;
import java.util.List;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class DisplayCards extends Application {
    
    HBox imageViews;

    
    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        boolean[] cards = new boolean[52];
        int count = 0;
        while(count <3) {
            int card = (int)(Math.random() * 52);
            
            if(!cards[card]) {
                cards[card] = true;
                pane.add(new ImageView(new Image("card/" + (card) + ".png")), count, 0);
                count++;
            }
        }
        imageViews = new HBox();
        imageViews.setAlignment(Pos.CENTER);
        
        shuffle();
    
        
        Button btnShuffle = new Button("Shuffle");
        btnShuffle.setOnAction(new EventHandler<ActionEvent>() {
            
            public void handle(ActionEvent event) {
                shuffle();
                
            }
        });
        
        BorderPane Bpane = new BorderPane();
        Bpane.setCenter(imageViews);
        Bpane.setBottom(btnShuffle);
        
        Scene scene = new Scene(pane, 250, 150);
        primaryStage.setTitle("Display 4 Cards");
        primaryStage.setScene(scene);
        primaryStage.show();
        
    }

    private void shuffle() {
        Collections.shuffle(cards);
        
    }

    public static void main(String[] args) {
        launch(args);
    }

    

}

【问题讨论】:

  • 请不要将您的代码发布为图片,而是将其作为文本直接发布在问题中,以Minimal, Complete, and Verifiable Example 的形式发布。
  • 顺便说一句,您可以将数组转换为 List 并随机播放。
  • 我刚把图片改成文字了
  • 我不会使用布尔数组来表示卡片。你能说出这个数组中有哪些卡吗? [真,真]和[真,真]?
  • @ChengThao 它使用文件扩展名为“.png”的文件中的卡片名称,从文件中的 51 张卡片中随机选择 3 张卡片。名称是“card1.png”、“card2.png”、“card3.png”等。

标签: java eclipse computer-science


【解决方案1】:

您可以为数组实现 Fisher-Yates 洗牌。

private void shuffle(){
    for(int i = cards.length - 1; i > 0; i--){
        int j = java.util.concurrent.ThreadLocalRandom.current().nextInt(i + 1);
        boolean temp = cards[i];
        cards[i] = cards[j];
        cards[j] = temp;
    }
}

【讨论】:

猜你喜欢
  • 2016-04-09
  • 2016-03-09
  • 1970-01-01
  • 2022-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 2016-02-02
  • 2014-06-29
相关资源
最近更新 更多