作者ta395ki2 (沒關係繼續努力)
看板Programming
標題[問題] 老師出的java作葉
時間Sat Dec 9 18:11:09 2006
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
噓 superGA:葉 61.229.232.44 12/09 19:09
推 meltice:我知道C++有random_shuffle可以用 218.211.11.155 12/09 21:42
→ ta395ki2:random_shuffle這是打亂 但還是會重複 59.125.204.142 12/09 22:06
→ ta395ki2:我在想想 59.125.204.142 12/09 22:16
推 meltice:先把1~52放到陣列 然後再打亂 OK 218.211.16.19 12/10 09:05