[問題] 老師出的java作葉
import javax.swing.*;
import java.awt.*;
public class s {
public static void main(String[] args){
int f;
JFrame frame = new JFrame("s(FlowLayout)");
Container c=frame.getContentPane();
FlowLayout f1=new FlowLayout(FlowLayout.LEFT);
c.setLayout(new GridLayout(7,7,15,15));
for(int p=1;p<=52;p++){
f=(int)(Math.random()*52)+1;
c.add(new JButton("w"+f));
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,400); //設定視窗大小
frame.pack();
frame.setVisible(true); //顯示視窗
}
}
這樣執行出來的話 會出現52個按鈕 然後這52個按鈕會隨機出現亂數 1~52
可是卡在 這52個按鈕的數字 會重複到 (老師說不能有重複!!!)
問題:
怎麼才能讓這52個按鈕的數字 隨機出現1~52的數字 且不會有" 重複 "的!!
!!!各位高手拜託指點一下 @@
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.125.204.142
噓
12/09 19:09, , 1F
12/09 19:09, 1F
推
12/09 21:42, , 2F
12/09 21:42, 2F
→
12/09 21:48, , 3F
12/09 21:48, 3F
→
12/09 22:06, , 4F
12/09 22:06, 4F
→
12/09 22:16, , 5F
12/09 22:16, 5F
推
12/10 09:05, , 6F
12/10 09:05, 6F
討論串 (同標題文章)
Programming 近期熱門文章
PTT數位生活區 即時熱門文章