按照你的要求编写的Java程序如下:
成都创新互联专注于企业营销型网站建设、网站重做改版、文成网站定制设计、自适应品牌网站建设、H5网站设计、商城建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为文成等各大城市提供网站开发制作服务。
public class CCH {
public static void main(String[] args) {
int sum=0;
float average;
System.out.print("随机列出十个数字是:");
for(int i=0;i10;i++){
int n=(int)(Math.random()*100);
sum=sum+n;
System.out.print(n+" ");
}
System.out.println();
average=sum/10.0f;
System.out.println("它们的和为:"+sum);
System.out.println("它们的平均数为"+average);
}
}
运行结果:
随机列出十个数字是:58 45 43 32 58 15 73 73 71 76
它们的和为:544
它们的平均数为54.4
import java.util.*;
public class NumOption {
public static void main(String[] args) {
int sum = 0;
int i = 0;
int count= 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入整数,以0结尾");
do {
System.out.print("请输入数字:");
i = sc.nextInt();
sum += i;
count ++;
} while (i != 0);
System.out.println("共输入了" + --count + "个数字, 和为" + sum + ", 平均数为" + (double)sum / count);
}
}
package test;
import java.util.List;
public interface Average{
public double getAverage(ListDouble list);
}
package test;
import java.util.List;
public class First implements Average {
public double getAverage(ListDouble list) {
double sum = 0;
for(int i=0;ilist.size();i++){
sum += list.get(i).doubleValue();
}
return sum / list.size();
}
}
package test;
import java.util.Collections;
import java.util.List;
public class Second implements Average {
public double getAverage(ListDouble list) {
Collections.sort(list);
double sum = 0;
for(int i=1;ilist.size()-1;i++){
sum += list.get(i).doubleValue();
}
return sum / (list.size() - 2);
}
}
package test;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args){
ListDouble list = new ArrayListDouble();
list.add(50d);
list.add(48d);
list.add(20d);
list.add(51d);
list.add(36d);
Average first = new First();
Average second = new Second();
System.out.println(first.getAverage(list));
System.out.println(second.getAverage(list));
}
}
为了测试,我只输入了5个数,录入的代码网上很多,算法给出了,祝你好运。
public
class
Average{
public
static
void
main(String[]
args){
//自己定义要求的数
double[]
nums
=
{1,
2.5,
5};
System.out.println("平均数为:"
+
average(nums));
}
public
static
double
average(double[]
nums){
double
num=0.0;
for(int
i=0;
inums.length;
i++){
num+=nums[i];
}
return
num/nums.length;
}
}
参考代码如下:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int count = 0;
while(true){
int num = sc.nextInt();
if(num == 0) break;
sum += num;
count++;
}
System.out.println("平均值:"+sum*1.0/count);
}
}
运行结果:
public class aveage {
public static void main(String[] args) {
int[] arrary1 = { 12,4,54,6,78,98,48,787,456 };
double x = ave(arrary1);
System.out.println("平均数为" + x);
}
static double ave(int[] a) {
int sum = 0;
for (int i = 0; i a.length; i++) {
sum += a[i];//累加求和
}
return sum*1.0/a.length;//求平均数
// *1.0的左右,就是把结果类型变成double类型
}
}