查看完整版本: 模擬擲骰子100次,求各點出現的次數
頁: [1]

b0919939849 發表於 2009-5-28 05:14 PM

模擬擲骰子100次,求各點出現的次數

模擬擲骰子100次,求各點出現的次數?
答:
import java.util.Random;
public class test {
  public static void main(String[] args) {
    int[] faceCount = new int;
    Random rand = new Random();
    for(int i=0; i<100; i++) {
      int face = rand.nextInt(6) + 1; // 亂數從 1 到 6
      faceCount++;
    }
    for(int i=0; i<faceCount.length; i++) {
      System.out.println((i+1) + "點出現次數 : " + faceCount);
    }
  }
}


真的是這樣媽?...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

csihcs 發表於 2009-5-28 07:03 PM

模擬擲骰子100次,求各點出現的次數?
b0919939849 發表於 2009-5-28 17:14 http://www02.eyny.com/images/common/back.gif
import java.util.Random;
public class test {
  public static void main(String[] args) {
    int[] faceCount = new int;
    Random rand = new Random();
    for(int i=0; i<100; i++) {
//      int face = rand.nextInt(6) + 1; // 亂數從 1 到 6
//      faceCount++;
// 上面兩行可以合併在一起變成
       faceCount++;
    }
    for(int i=0; i<faceCount.length; i++) {
//      System.out.println((i+1) + "點出現次數 : " + faceCount);
// faceCount 要改成 faceCount,否則只會印出faceCount 這個整數陣列物件的 toString,類似 [I@c17164 的樣子
      System.out.println((i+1) + "點出現次數 : " + faceCount);
    }
  }
}
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

jimpig1227 發表於 2009-5-31 12:16 AM

又學到了一招,謝謝分享,讓我這個新手功力又進步了
頁: [1]