在公交站等车,有两路公交可乘,其中一路每隔10分钟发一辆,另一路公交每隔15分钟发一辆,设两路公交车发车时间相互独立,问等车时间的数学期望。
(35/9)
1 import java.lang.Math;
2 import java.math.BigInteger;
3 import java.text.DecimalFormat;
4 import java.util.Scanner;
5
6 public class Dengche {
7
8 public static void main(String[] args) {
9 int k;
10 Scanner scan = new Scanner(System.in);
11 System.out.println("请输入1到" + Integer.MAX_VALUE + "之间的数字");
12 System.out.print("输入实验次数:");
13 try {
14 k = scan.nextInt();
15 m1(k);
16 } catch (Exception e) {
17 System.out.println("输入不正确 或者 输入的数字超出" + Integer.MAX_VALUE);
18 System.out.println("异常类型 :" + e);
19 }
20
21 }
22
23 public static String m1(int k1) {
24
25 double[] a = new double[k1];
26 double sum = 0.0;
27 for (int i = 0; i < k1; i++) {
28
29 double time1 = Math.random() * 10;
30 double time2 = Math.random() * 15;
31 double time = Math.min(time1, time2);
32 a[i] = time;
33 sum += a[i];
34 }
35
36 double average = sum / (double) k1;
37
38 DecimalFormat ave = new DecimalFormat("######0.00");
39 System.out.println("距离下一辆车到达还有" + ave.format(average) + "分钟");
40 String b = ave.format(average);
41 return b;
42 }
43
44 }