【发布时间】:2013-12-30 18:47:09
【问题描述】:
我的问题是关于在我的程序计算算术平均值时丢弃最大和最小的数字。
这是我的代码示例:
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];
}
}
此代码示例对我有帮助吗?
【问题讨论】:
-
当您遍历循环添加数字时,检查最大和最小数字。然后在循环完成后,从总和中减去这些数字。像 PI 一样简单。
-
你能打开它吗?因为我不明白该怎么做。在代码示例上显示对我有好处。@HovercraftFullOfEels
-
@Hovercraft Full Of Eels:如果最大或最小的数字出现多次,这将不起作用。
-
TheSmallestNumber 最好让函数名称以小写开头,例如 theSmallestNumber
-
jtf.acm.org/tutorial/Introduction.html 这是 ACM 库?