![]() |
||
|
Junior Member
![]() ![]() ![]() 加入日期: Jul 2004
文章: 871
|
一個困擾我3天二夜的java程式碼
class RandInt2
{ static int[] setupArray(int n) { int a[] = new int[n]; for (int i=0; i<a.length; i++) a[i] = (int)(Math.random()*10); return a; } static String showArray(int[] a) { String s = "建立" + a.length + "個隨機數如下:"; for (int i=0; i<a.length; i++) { if (i%10==0) s = s + "\n"; else s = s + a[i] + " "; } s = s + "\n"; return s; } public static void main(String args[]) { int a[] = setupArray(100); System.out.println(showArray(a)); // int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for (int i=0; i<a.length; i++) count[a[i]]++; System.out.println("隨機數\t次數\n------\t----"); for (int i=0; i<count.length; i++) System.out.println(i+"\t"+count[i]); } } 要改成讓他隨機出現20個數字 每個數字範圍為1~6 每五個數字為一行 所以是四行 還要統計每個數字出現的次數>"< 寫了三天二夜!還是寫不出來~請問有哪個大大可以幫助我的!! |
|||||||
|
|
|
Advance Member
![]() ![]() 加入日期: Jun 2003 您的住址: Taipei
文章: 442
|
你的程式分為 3 個靜態函式, setupArray 在產生特定數量,特定範圍的整數;showArray 則在列印 setupArray 所產生的陣列;最後 main 統計 1~6 出現的次數,再印出來。
只要先看懂程式碼,要改就很簡單了。 此文章於 2005-11-09 09:08 PM 被 harrisonlin 編輯. |
||
|
|
|
Major Member
![]() 加入日期: Oct 2004 您的住址: 高雄的大醫院附近
文章: 216
|
又到了開始求程式的時候了嗎??
這程式答案已經在裡面了,真的還要寫3天??? class RandInt2 { static int[] setupArray(int n) { int a[] = new int[n]; for (int i=0; i<a.length; i++) a[i] = (int)(Math.random() % 6); if(a[i] == 0) a[i] == 6; return a; } static String showArray(int[] a) { String s = "建立" + a.length + "個隨機數如下:"; for (int i=0; i<a.length; i++) { if (i%5==0) s = s + "\n"; else s = s + a[i] + " "; } s = s + "\n"; return s; } public static void main(String args[]) { int a[] = setupArray(20); System.out.println(showArray(a)); int count[] = {0, 0, 0, 0, 0, 0}; for (int i=0; i<a.length; i++) count[a[i] % 6]++; for (int i=1; i<count.length; i++) System.out.println(i+"\t"+count[i]); System.out.println(6+"\t"+count[0]); } 我已經半年沒有寫程式了 ![]()
__________________
過去的荒唐 好男人壞男人,還不都是你們害的、原來Y拍已經是廠商才能呆的地方了 最近的誑語 [感想]給8點檔的編劇們(三、民)、 [大科技廠]某美面板的cost down、 理論上來說,最極品的妹 |
|
|