从键盘输入10个数,输出这些数中大于其平均值的数,用java编写

2024-11-10 15:05:14
推荐回答(2个)
回答1:

public class T {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
double s = 0;
System.out.println("请输入10个数:");
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = scanner.nextInt();
sum += a[i];
}
s = sum / a.length;
System.out.println("大于平均值的值如下:");
for (int i = 0; i < a.length; i++) {
if (a[i]>s) {
System.out.print(a[i]+" ");
}
}
}
}

回答2:

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int num[] = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {

num[i] = sc.nextInt(); }
double avg;
long sum = 0;
for (int i = 0; i < num.length; i++) {
sum = sum + num[i];
}
avg = sum / 10;

for (int i = 0; i < num.length; i++) {
if (num[i] > avg)

System.err.println("大于平均数的值为:"+num[i]);
}
}

}