Board logo

標題: 亂數 (三) [打印本頁]

作者: 許婷芳    時間: 2019-9-27 16:54     標題: 亂數 (三)

本帖最後由 許婷芳 於 2019-9-28 14:00 編輯

產生20組範圍介於1~3之隨機亂數(整數).
  1. public class Ch32
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         for(int i=1; i<=20; i++)
  6.             System.out.println((int)(Math.random()*3+1));
  7.     }
  8. }
  9. /*
  10.   Math.random()             0.0000 ~ 0.999999
  11.   Math.random()*3           0.0000 ~ 2.999999
  12.   Math.random()*3+1         1.0000 ~ 3.999999
  13.   (int)(Math.random()*3+1)  1 ~ 3
  14. */
複製代碼

作者: 吳孟修    時間: 2019-9-28 14:02

  1. public class a
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         for(int i=1; i<=20; i++)
  6.             System.out.println((int)(Math.random()*3+1));
  7.     }
  8. }
複製代碼

作者: 陳柏銓    時間: 2019-9-28 14:03

  1. public class C05 {

  2.         public static void main(String[] args) {
  3.                 double t;
  4.                 for(int i=0;i<20;i++)
  5.                 {
  6.                    System.out.println((int)Math.random());
  7. }
  8.                
  9.         }

  10. }
複製代碼

作者: 吳孟書    時間: 2019-9-28 14:04

  1. public class a
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         for(int i=1; i<=20; i++)
  6.               System.out.println((int)(Math.random()*3+1));
  7.     }
  8. }
複製代碼

作者: 田宇任    時間: 2019-9-28 14:04

  1. public class king
  2. {
  3.   public static void main(String args[])
  4.   {
  5.     for(int x=1;x<=20;x++)
  6.      System.out.println((int)Math.random()*3+1);
  7.   }
  8. }
複製代碼

作者: 洪寬瀧    時間: 2019-9-28 14:05

  1. public class Ch31
  2. {
  3.     public static void main(String args[])
  4.     {

  5.       for(int i=1;i<=20;i++)
  6.       {
  7.       system.out.println((int)Math.random()*3+1);
  8.       }

  9.     }
  10. }
複製代碼

作者: 湯郡一    時間: 2019-9-28 14:06

  1. public class Ch03
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         for(int i=1; i<=20; i++)
  6.             System.out.println((int)(Math.random()*3+1));
  7.     }
  8. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2