所以现在计算平均值,我试图打印出用户输入,只在其自己的行中严格低于平均值。我尝试这样做,但是,错误“二元运算符的坏操作数”<“。
const std::array<int, 3> array1 = {{1, 2, 3}};
const std::array<int, 2> array2 = {{4, 5}};
const std::array<int, 4> array3 = {{6, 7, 8, 9}};
const auto result = concatenate(array1, array2, array3);
答案 0 :(得分:1)
for (int i = howMany - 1; i >= 0; i--){
sum+=myArray[i];//sum up the array
}
avg = sum / howMany;
要在一行中打印每个double
:
System.out.println(avg);
for (int i = 0; i < howMany; i++) {
if(myArray[i] < avg){
System.out.println(myArray[i]);//single line for every double
}
}
在一行中打印所有double
:
String oneLine = "[";
int lowerAvg = 0;
for (int i = 0; i < howMany; i++) {
if(myArray[i] < avg){
oneLine += (i > 0 && ++lowerAvg > 0)?", ":""; // add separator
oneLine += (myArray[i]);
}
}
oneLine+="]";
System.out.println(oneLine);//prints like [1.0, 2.0, ...]
使用Java 8:
avg = Arrays.stream(myArray, 0, howMany).average().orElse(0);
final double finalAvg = avg;//final variable to use in filter condition
System.out.println(finalAvg);
Arrays.stream(myArray, 0, howMany).filter(x -> x < finalAvg).forEach(System.out::println);//will print every double in a single line
System.out.println(Arrays.toString(Arrays.stream(myArray, 0, howMany).filter(x -> x < finalAvg).toArray()));//Will print the array like [1.0, 2.0, ...]