gpt4 book ai didi

java - 如何计算代码示例中的算术平均值?

转载 作者:行者123 更新时间:2023-12-01 23:50:24 26 4
gpt4 key购买 nike

我编写了一个关于计算算术平均值的代码示例,我做了很多事情,但有些部分丢失/不正确。

这是我的代码示例:

public static void main(String[] args) {
int i;
int j;
int thelargest;
int thesmallest;

Scanner input = new Scanner(System.in);
System.out.println("Enter the list of number : ");
String input2 = input.nextLine();

String[] numbers = input2.split("\\s+");

int[] result = new int[numbers.length];
for (j = 0; j < numbers.length; j++) {
result[j] = Integer.parseInt(numbers[j]);

}

for (i = 0; i < result.length; i++) {
System.out.print("");
System.out.println(result[i]);
}

System.out.println("The Largest Number : "
+ findTheLargestNumber(result));
System.out.println("The Smallest Number : "
+ findTheSmallestNumber(result));
thelargest = findTheLargestNumber(result);
thesmallest = findTheSmallestNumber(result);
float arithmeticMean = (float) (result[i + j])// result.length;
System.out.println("The Arithmetic Mean : " + arithmeticMean);

/*有一个错误,我试图解决它,但我没有找到任何方法来解决它。我希望我的程序对结果(数字)求和并除以结果数。(例如:10 +20+30=60,算术平均值为60/3=20。)最后,我认为错误在于(float)(结果[i+j])。

*/

}

public static int findTheSmallestNumber(int[] series) {
int thesmallest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] < thesmallest) {

thesmallest = series[i];
}
}
return thesmallest;
}

public static int findTheLargestNumber(int[] series) {
int thelargest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] > thelargest) {

thelargest = series[i];
}
}
return thelargest;
}

}

感谢所有帮助我的人。

最佳答案

public static float getAirthmeticMean(int[] result){
int sum = 0;
for (int i = 0; i < result.length; i++) {
sum+=result[i]);
}
return (float)sum/result.length;
}

关于java - 如何计算代码示例中的算术平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16385133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com