英文:
How do I get the mean of these variables..... What is the arithmetic function?
问题
如何编写一个方法来计算一些数字的平均值?
public class MyCalculator {
int input1 = 10;
int input2 = 20;
int input3 = 30;
int input4 = 40;
int input5 = 50;
public float average() {
// TODO: 编写Java代码来计算所有输入变量的平均值
return 0;
}
}
英文:
How can I write a method to calculate the average of some numbers?
public class MyCalculator {
int input1 = 10;
int input2 = 20;
int input3 = 30;
int input4 = 40;
int input5 = 50;
public float average() {
// TODO: write java code to calculate the average for all input variables
return 0;
}
}
答案1
得分: 0
以下是您要求的代码部分翻译:
你可以尝试类似这样的代码,使用了新的 `Stream` 库。
import java.util.stream.*;
class MyCalculator {
public static double average(int[] input) {
return IntStream.of(arr).average().getAsDouble();
}
public static void main(String[] args) {
int[] input = {10, 20, 30, 40, 50};
System.out.println(average(input));
}
}
输出结果:
30.0
英文:
You could go for something like this, which uses the new Stream
library.
import java.util.stream.*;
class MyCalculator {
public static double average(int[] input) {
return IntStream.of(arr).average().getAsDouble();
}
public static void main(String[] args) {
int[] input = {10, 20, 30, 40, 50};
System.out.println(average(input));
}
}
Output:
30.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论