我正在尝试写一个程序,用户将把数字,我需要得到最接近的数字平均值。
//average
for (i =0; i<arr.length; i++){
total = total + arr[i];
}
double average = total/arr.length;
System.out.println("Average is: " + average);
那我就试着这么做;
//more close to average
for (i=0; i<arr.length; i++) {
if (arr[i] > average) {
System.out.println("Numbers above average: " + arr[i]);
} else if (arr[i] < average) {
System.out.println("Numbers below average; " + arr[i]);
}else
System.out.println("Wrong!");
}
我的问题是,当我输入数字1 12 17 23 62时,平均值是正确的:23.0,但输出是这样的:
Numbers below average; 1
Numbers below average; 12
Numbers below average; 17
Numbers below average; 23
Numbers below average; 62
为什么62号后不传递给else if语句?
多谢
所有代码
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i;
double total = 0;
System.out.println("Indicate number of values: ");
int [] arr = new int[scanner.nextInt()];
System.out.println("Indicate numbers: ");
for (i = 0; i<arr.length; i++){
int num = scanner.nextInt();
arr[i] = num;
}
//average
for (i =0; i<arr.length; i++){
total = total + arr[i];
}
double average = total/arr.length;
System.out.println("Average is: " + average);
total = 0;
//derivation
for (i =0; i<arr.length; i++){
total += Math.pow((arr[i] - average), 2);
}
average = total/(arr.length-1);
double deviation = Math.sqrt(average);
//variance
double variance = Math.pow((deviation),2);
System.out.println("Variance is: "+ variance);
//more close to average
for (i=0; i<arr.length; i++) {
if (arr[i] > average) {
System.out.println("Numbers above average: " + arr[i]);
} else if (arr[i] < average) {
System.out.println("Numbers below average; " + arr[i]);
}else
System.out.println("Wrong!");
}
}
}
import java.util.Vector;
public class Main
{
public static void main(String[] args) {
int[] arr = {1, 12, 17, 23, 62};
Vector<Integer> BelowAvg = new Vector<Integer>();
Vector<Integer> AboveAvg = new Vector<Integer>();
Vector<Integer> Avg = new Vector<Integer>();
int total = 0;
for (int i = 0; i < arr.length; i++){
total += + arr[i];
}
double average = total/arr.length;
System.out.println("Average is: " + average);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > average) {
AboveAvg.add(arr[i]);
} else if (arr[i] < average) {
BelowAvg.add(arr[i]);
} else
Avg.add(arr[i]);
}
System.out.print("Numbers above average: ");
for (int i = 0; i < AboveAvg.size() - 1; i++) {
System.out.print(AboveAvg.get(i) + ", ");
}
System.out.println(AboveAvg.get(AboveAvg.size() - 1));
System.out.print("Numbers below average: ");
for (int i = 0; i < BelowAvg.size() - 1; i++) {
System.out.print(BelowAvg.get(i) + ", ");
}
System.out.println(BelowAvg.get(BelowAvg.size() - 1));
System.out.print("Numbers equal to average: ");
for (int i = 0; i < Avg.size() - 1; i++) {
System.out.print(Avg.get(i) + ", ");
}
System.out.println(Avg.get(Avg.size() - 1));
}
}
我做了这个,我认为它符合你的要求。
问题是您正在重新计算total
和avere
值,再次打印该值,您将看到大于62:
您需要将代码更改为以下内容:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Indicate number of values: ");
int [] arr = new int[scanner.nextInt()];
System.out.println("Indicate numbers: ");
for (int i = 0; i<arr.length; i++){
arr[i] = scanner.nextInt();
}
//average
double total = 0;
for (int i =0; i<arr.length; i++){
total = total + arr[i];
}
double average = total/arr.length;
System.out.println("Average is: " + average);
total = 0;
//derivation
for (int i =0; i<arr.length; i++){
total += Math.pow((arr[i] - average), 2);
}
double deviation = Math.sqrt(total/(arr.length-1));
//variance
double variance = Math.pow((deviation),2);
System.out.println("Variance is: "+ variance);
//more close to average
for (i=0; i<arr.length; i++) {
if (arr[i] > average) {
System.out.println("Numbers above average: " + arr[i]);
} else if (arr[i] < average) {
System.out.println("Numbers below average; " + arr[i]);
}else
System.out.println("Wrong!");
}
}
}
我想计算最接近数组平均数的数字,我已经计算完平均数,但我不知道如何找到最接近平均数的数字。 }
问题内容: 我不确定是否曾经问过这个问题,但是如何在T-SQL中将平均值四舍五入到最接近的整数? 问题答案: 这为它工作: 难道没有更短的方法吗?
问题内容: 我有一系列正/负整数 现在,我想针对此数组测试另一个int,并返回最接近该int的数字。 例如,如果我使用数字,我将从数字中取回第4项,那么做这种事情的最佳方法是什么? 那不行 有什么好的方法建议吗? 问题答案: 始终使用要考虑的第一个元素初始化最小/最大函数。使用诸如或这样的东西是获得答案的幼稚方式;如果以后再更改数据类型(糟糕,而且有很大不同!),或者将来您想为 任何 数据类型编写
问题内容: 是否有numpy-thonic方法(例如函数)在数组中查找最接近的值? 例: 问题答案:
问题内容: 我很难找出例如如何从列表中查找分钟 如何通过定义()函数来查找此列表的最小值和最大值 我不想使用内置功能 问题答案: 如果要手动查找最小值作为函数: Python 3.4引入了该软件包,该软件包提供了其他统计信息: