![]() |
PCDVD數位科技討論區
(https://www.pcdvd.com.tw/index.php)
- 七嘴八舌異言堂
(https://www.pcdvd.com.tw/forumdisplay.php?f=12)
- - 一個困擾我3天二夜的java程式碼
(https://www.pcdvd.com.tw/showthread.php?t=557604)
|
|---|
一個困擾我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 每五個數字為一行 所以是四行 還要統計每個數字出現的次數>"< 寫了三天二夜!還是寫不出來~請問有哪個大大可以幫助我的!! |
你的程式分為 3 個靜態函式, setupArray 在產生特定數量,特定範圍的整數;showArray 則在列印 setupArray 所產生的陣列;最後 main 統計 1~6 出現的次數,再印出來。
只要先看懂程式碼,要改就很簡單了。 |
又到了開始求程式的時候了嗎??
這程式答案已經在裡面了,真的還要寫3天??? :jolin: 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]); } 我已經半年沒有寫程式了 :think: |
| 所有的時間均為GMT +8。 現在的時間是07:17 AM. |
vBulletin Version 3.0.1
powered_by_vbulletin 2026。