[問題] 老師出的java作葉

看板Programming作者 (沒關係繼續努力)時間18年前 (2006/12/09 18:04), 編輯推噓1(213)
留言6則, 3人參與, 最新討論串1/7 (看更多)
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
我知道C++有random_shuffle可以用
12/09 21:42, 2F

12/09 21:48, , 3F
幫你找到java的 http://0rz.tw/702db
12/09 21:48, 3F

12/09 22:06, , 4F
random_shuffle這是打亂 但還是會重複
12/09 22:06, 4F

12/09 22:16, , 5F
我在想想
12/09 22:16, 5F

12/10 09:05, , 6F
先把1~52放到陣列 然後再打亂 OK
12/10 09:05, 6F
文章代碼(AID): #15UeeueL (Programming)
文章代碼(AID): #15UeeueL (Programming)