看板 Programming 關於我們 聯絡資訊
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
meltice:幫你找到java的 http://0rz.tw/702db 218.211.11.155 12/09 21:48
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