Java設計實踐課練習題

來源:互聯網
上載者:User

標籤:gif   實踐   ima   sed   ack   code   ide   sys   exp   

 1 package hello; 2 import java.util.*; 3  4 public class Hello { 5     public static void main(String args[]){ 6         double[] a = produce(10000); 7         int[] b = tongJi(a); 8         for (int e : b) { 9             System.out.print(e +" ");10         }11         System.out.println();12         double exp = expect(a);13         System.out.println(exp);14         double f = fangCha(exp, a);15         System.out.println(f);16         17     }18     public static double[] produce(int length) {19         double[] a = new double[length];20         for (int i = 0; i < length; i++) {21             a[i] = Math.random() * 2 - 1;22         }23         return a;24     }25     public static int[] tongJi(double a[]) {26         int[] b = new int[20];27         for (int i = 0; i < a.length; i++) {28             for (int j = 0; j < b.length; j++) {29                 if (a[i] > -1 + 0.1 * j && a[i] < -1 + 0.1 * (j + 1)) {30                     b[j]++;31                 }32             }33         }34         return b;35     }36     public static double expect(double b[]) {37         double sum = 0;38         for (int i = 0; i < b.length; i++) {39             sum = sum + b[i];40         }41         double exp = sum/10000;42         return exp;43     }44     public static double fangCha(double exp, double a[]) {45         double sum = 0;46         for (int i = 0; i < a.length; i++) {47             sum = sum + (a[i] - exp) * (a[i] - exp);48         }49         double f = Math.sqrt(sum/10000);50         return f;51     }52 }
View Code

 1 package hello; 2 import java.util.*; 3  4 public class Hello { 5     public static void main(String args[]){ 6         double[] a = produce(10000); 7         int[] b = tongJi(a); 8         for (int e : b) { 9             System.out.print(e +" ");10         }11         System.out.println();12         double exp = expect(a);13         System.out.println(exp);14         double f = fangCha(exp, a);15         System.out.println(f);16         17     }18     public static double[] produce(int length) {19         double[] a = new double[length];20         for (int i = 0; i < length; i++) {21             a[i] = getNumRandom();22         }23         return a;24     }25     public static int[] tongJi(double a[]) {26         int[] b = new int[20];27         for (int i = 0; i < a.length; i++) {28             for (int j = 0; j < b.length; j++) {29                 if (a[i] > -6 + 0.6 * j && a[i] < -6 + 0.6 * (j + 1)) {30                     b[j]++;31                 }32             }33         }34         return b;35     }36     public static double expect(double b[]) {37         double sum = 0;38         for (int i = 0; i < b.length; i++) {39             sum = sum + b[i];40         }41         double exp = sum/10000;42         return exp;43     }44     public static double fangCha(double exp, double a[]) {45         double sum = 0;46         for (int i = 0; i < a.length; i++) {47             sum = sum + (a[i] - exp) * (a[i] - exp);48         }49         double f = Math.sqrt(sum/10000);50         return f;51     }52     public static double getNumRandom() {53         double sum = 0;54         for (int i = 0 ; i < 12; i++) {55             sum = sum + Math.random();56         }57         return sum - 6;58     }59 }
View Code

 

Java設計實踐課練習題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.