生成数组,如:(56,42).…(xy)共10组数据,生成时XY取1-100间的随机整数;
计算所有array成员与其后各array成员之间的坐标斜率(v1-y2)/(x1-x2),取绝对值且保留两位小数(如array[0]分别与array[1]-array[9]做计算,array[2]与 array[3]-array[9]做计算,以此类推);
获取所有斜率的平均值及中位数(中位数:所有斜率按大小排列,取排在最中间的数值);输出数组结果、斜率明细值及最终结果;
试着写了一下,仅供参考
public static void main( String[] args ) {
method();
}
private static void method(){
List<List<Integer>> lists = new ArrayList<>();
for (int i = 0; i < 10; i++) {
lists.add(getRandomList());
}
List<Double> doubleList = new ArrayList<>();
for (int i = lists.size() - 1; i >= 0; i-- ) {
List<Integer> list = lists.get(i);
for (int j = i+1; j < lists.size() ; j++) {
List<Integer> list1 = lists.get(j);
int x = list.get(0) - list1.get(0);
int y = (list.get(1)-list1.get(1));
double xy = 0.00;
if (x!=0 && y!=0){
BigDecimal divide = BigDecimal.valueOf(x).divide(BigDecimal.valueOf(y),2,BigDecimal.ROUND_HALF_EVEN);
xy = divide.doubleValue();
}
doubleList.add(xy);
System.out.println("第"+(i+1)+"组坐标与第"+(j+1)+"组坐标的斜率为 = " + xy);
}
}
Double collect = doubleList.stream().collect(Collectors.averagingDouble(Double::doubleValue));
DecimalFormat decimalFormat = new DecimalFormat ("0.00");
String format = decimalFormat.format(collect);
System.out.println("所有斜率的平均值为 = " + format);
List<Double> collect1 = doubleList.stream().sorted().collect(Collectors.toList());
int i = collect1.size() / 2;
Double aDouble = collect1.get(i);
System.out.println("中位数为 = " + aDouble);
}
private static List<Integer> getRandomList(){
List<Integer> l = new ArrayList<>();
l.add(RandomUtils.nextInt(1,100));
l.add(RandomUtils.nextInt(1,100));
return l;
}