首页 > 代码库 > Java设计实践课练习题

Java设计实践课练习题

技术分享

技术分享
 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设计实践课练习题