首页 > 代码库 > 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 }
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 }
Java设计实践课练习题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。