好的,所以我是 ArrayList 的新手,我想做的是制作一个程序,从一副 54 张卡片中随机获取 3 张卡片,并且没有任何重复。我不知道在我的 if 循环中放什么。请帮忙
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.ArrayList;
public class card_ran1 extends JFrame
{
public card_ran1()
{
ArrayList<Integer> Ran = new ArrayList<Integer>();
setLayout(new GridLayout(1,4,5,5));
Random random = new Random();
int i = random.nextInt(54) + 1 ;
int n = random.nextInt(54) + 1 ;
int m = random.nextInt(54) + 1 ;
Ran.add(i);
if (Ran.contains(n))
{
//what should go here
}
if (Ran.contains(m))
{
//what should go here
}
add(new JLabel(new ImageIcon("card/" + Ran.get(0) + ".png")));
add(new JLabel(new ImageIcon("card/" + Ran.get(1) + ".png")));
add(new JLabel(new ImageIcon("card/" + Ran.get(2) + ".png")));
}
public static void main(String[] args)
{
card_ran1 frame = new card_ran1();
frame.setTitle("Random Cards");
frame.setSize(600,300);
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}