java输入数平均值代码,java编写算平均数程序
java编写一个程序输入N个整数,求其和、平均值、最大值、最小值?
package test20210106;
成都网络公司-成都网站建设公司创新互联十多年经验成就非凡,专业从事网站设计制作、成都网站建设,成都网页设计,成都网页制作,软文平台,1元广告等。十多年来已成功提供全面的成都网站建设方案,打造行业特色的成都网站建设案例,建站热线:18980820575,我们期待您的来电!
import java.util.Arrays;
import java.util.Scanner;
public class Test01 {
static int[] arr = new int[0];
public static void main(String[] args) {
systemIn();
System.out.println("数组"+Arrays.toString(arr)+" 的各项数据如下: ");
System.out.println("总和为: "+getSum());
double d = (double)getSum()/(double)arr.length;
System.out.println("平均值: "+d);
sort();
System.out.println("最大值: "+arr[0]);
System.out.println("最小值: "+arr[arr.length-1]);
}
/**用户输入数据*/
public static void systemIn(){
System.out.println("请输入一个整数,输入0则表示输入结束,程序会执行计算程序");
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
if(q!=0){
arr = Arrays.copyOf(arr, arr.length+1);
arr[arr.length-1] = q;
systemIn();
}
}
/**求和*/
public static int getSum(){
int sum = 0;
for (int i = 0; i arr.length; i++) {
sum += arr[i];
}
return sum;
}
/**给数组排序*/
public static void sort(){
for (int i = 0; i arr.length; i++) {
for (int j = arr.length-1; j i; j--) {
if(arr[j]arr[i]){
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
}
java输入数组,求平均值,求标准值!
以int类型数组为例,其他类型的都类似.
用户输入的时候用逗号隔开数字就可以了
import java.util.Scanner;
public class Baidu {
public static void main(String[] args) {
int[] ary = getArrayFromInput();
double average = calcArrayAverage(ary);
System.out.println("Average is: " + average);
int count = 0;
for (int item : ary) {
if (item average) {
count++;
}
}
System.out.println("Total " + count
+ " numbers greater than average of given array");
}
private static int[] getArrayFromInput() {
System.out.println("Please input some digits, separate with ',': ");
Scanner scanner = new Scanner(System.in);
String[] digits = scanner.nextLine().split(",");
int[] array = new int[digits.length];
for(int i = 0; i digits.length; i++){
array[i] = Integer.parseInt(digits[i].trim());
}
return array;
}
private static double calcArrayAverage(int[] ary) {
double arySum = 0D;
for (int item : ary) {
arySum += item;
}
return arySum / ary.length;
}
}
-----------
Please input some digits, separate with ',':
12, 35, 28, 127, 96, 35
Average is: 55.5
Total 2 numbers greater than average of given array
java用循环编写一个计数程序输入几个数字直到输入0为止,计算输入的这些数的平均数?
参考代码如下:
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);
}
}
运行结果:
java语言从控制台随意输入10个数·求这十个数的平均数
public
static
void
main(String
[]
arg){
//若想数字为手动输入,改为控制台输入语句即可!接受一个数组!
int
[]
temp={12,243,121,32,33,13,34,54,45,65};
//定义一个变量保存数组数字的和!
int
sum=0;
//循环数组
将其相加
for(int
i=0;itemp.length;i++){
sum+=temp[i];
}
//
java中/代表除号
System.out.println("平均数为:"+sum/10);
}
本文名称:java输入数平均值代码,java编写算平均数程序
URL标题:http://pwwzsj.com/article/phcsdp.html