我的问题是关于在我的程序计算算术平均值时丢弃最大和最小的数字。
这是我的代码示例:
public static void main(String[] args) {
int k;
int r;
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(" ");
int[] result = new int[numbers.length];
for (r = 0; r < numbers.length; r++) {
result[r] = Integer.parseInt(numbers[r]);
}
for (k = 0; k < result.length; k++) {
System.out.print("");
System.out.println(result[k]);
}
System.out.println(" LargestNumber : " + TheLargestNumber(result));
System.out.println(" SmallestNumber : " + TheSmallestNumber(result));
thelargest = TheLargestNumber(result);
thesmallest = TheSmallestNumber(result);
System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));
}
public static int TheSmallestNumber(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 TheLargestNumber(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 AirthmeticMean(int[] result) {
int sum = 0;
for (int i = 0; i < result.length; i++) {
sum += result[i];
}
return (float) sum / result.length;
}
我试图找到方法并编写了此示例,但我不知道如何嵌入此代码示例:
for (int i = 0; i < result.length; i++) {
if (series[i] != thesmallest && series[i] != thelargest) {
total = total + seriess[i];
}
}
此代码示例对我有帮助吗?