java编程:编程从键盘输入一维数组 int a[20]中的20个整数,并求此数组元素的最大值、最小值、平均值及所
编程从键盘输入一维数组inta[20]中的20个整数,并求此数组元素的最大值、最小值、平均值及所有元素之和。...
编程从键盘输入一维数组 int a[20]中的20个整数,并求此数组元素的最大值、最小值、平均值及所有 元素之和。
展开
展开全部
看看谁的效率高过我
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Cat {
public static void main(String args[]){
final int length = 20;
int[] ary = new int[length];
int sum = getDataAndCalcSumFromInput(ary);//填充数组,并且返回总和
Arrays.sort(ary);//从小到大升序排列数组
int min = ary[0];//最小值为第一个
int max = ary[length-1];//最大值必定为最后一个元素
double average = (double)sum / length;//求平均
System.out.println("Min is: " + min);
System.out.println("Max is: " + max);
System.out.println("Sum is: " + sum);
System.out.println("Average is: " + average);
}
private static int getDataAndCalcSumFromInput(int[] ary) {
int count = 0;
int sum = 0;
while(count < ary.length){
System.out.println("Please input an integer for array " + count );
Scanner scanner = new Scanner(System.in);
try {
ary[count] = scanner.nextInt();
sum += ary[count++];//累计和
} catch (InputMismatchException e) {
System.out.println("Invalid digit number in format, please input again!");
}
}
return sum;
}
}
---------------
Please input an integer for array 0
a
Invalid digit number in format, please input again!
Please input an integer for array 0
1
Please input an integer for array 1
cddd
Invalid digit number in format, please input again!
Please input an integer for array 1
2.5
Invalid digit number in format, please input again!
Please input an integer for array 1
0
Please input an integer for array 2
2
Please input an integer for array 3
3
Please input an integer for array 4
4
Please input an integer for array 5
5
Please input an integer for array 6
6
Please input an integer for array 7
7
Please input an integer for array 8
8
Please input an integer for array 9
9
Please input an integer for array 10
10
Please input an integer for array 11
11
Please input an integer for array 12
12
Please input an integer for array 13
13
Please input an integer for array 14
14
Please input an integer for array 15
15
Please input an integer for array 16
16
Please input an integer for array 17
17
Please input an integer for array 18
18
Please input an integer for array 19
19
Min is: 0
Max is: 19
Sum is: 190
Average is: 9.5
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Cat {
public static void main(String args[]){
final int length = 20;
int[] ary = new int[length];
int sum = getDataAndCalcSumFromInput(ary);//填充数组,并且返回总和
Arrays.sort(ary);//从小到大升序排列数组
int min = ary[0];//最小值为第一个
int max = ary[length-1];//最大值必定为最后一个元素
double average = (double)sum / length;//求平均
System.out.println("Min is: " + min);
System.out.println("Max is: " + max);
System.out.println("Sum is: " + sum);
System.out.println("Average is: " + average);
}
private static int getDataAndCalcSumFromInput(int[] ary) {
int count = 0;
int sum = 0;
while(count < ary.length){
System.out.println("Please input an integer for array " + count );
Scanner scanner = new Scanner(System.in);
try {
ary[count] = scanner.nextInt();
sum += ary[count++];//累计和
} catch (InputMismatchException e) {
System.out.println("Invalid digit number in format, please input again!");
}
}
return sum;
}
}
---------------
Please input an integer for array 0
a
Invalid digit number in format, please input again!
Please input an integer for array 0
1
Please input an integer for array 1
cddd
Invalid digit number in format, please input again!
Please input an integer for array 1
2.5
Invalid digit number in format, please input again!
Please input an integer for array 1
0
Please input an integer for array 2
2
Please input an integer for array 3
3
Please input an integer for array 4
4
Please input an integer for array 5
5
Please input an integer for array 6
6
Please input an integer for array 7
7
Please input an integer for array 8
8
Please input an integer for array 9
9
Please input an integer for array 10
10
Please input an integer for array 11
11
Please input an integer for array 12
12
Please input an integer for array 13
13
Please input an integer for array 14
14
Please input an integer for array 15
15
Please input an integer for array 16
16
Please input an integer for array 17
17
Please input an integer for array 18
18
Please input an integer for array 19
19
Min is: 0
Max is: 19
Sum is: 190
Average is: 9.5
展开全部
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void getValue()
{
int sumNumber = 0;
int[] arrayInt = new int[20];
Scanner s = new Scanner(System.in);
//循环输入十个数字
for(int i=0;i<20;i++){
System.out.println("输入第"+(i+1)+"个数字: ");
arrayInt[i] = s.nextInt();
sumNumber += arrayInt[i];
}
float avg = sumNumber/20;
//排序
Arrays.sort(arrayInt);
System.out.println("最大值:" + arrayInt[arrayInt.length-1]);
System.out.println("最小值:" + arrayInt[0]);
System.out.println("平均值:" + avg);
System.out.println("所有元素之和:"+sumNumber);
}
public static void main(String[] args) {
getValue();
}
}
import java.util.Scanner;
public class Test {
public static void getValue()
{
int sumNumber = 0;
int[] arrayInt = new int[20];
Scanner s = new Scanner(System.in);
//循环输入十个数字
for(int i=0;i<20;i++){
System.out.println("输入第"+(i+1)+"个数字: ");
arrayInt[i] = s.nextInt();
sumNumber += arrayInt[i];
}
float avg = sumNumber/20;
//排序
Arrays.sort(arrayInt);
System.out.println("最大值:" + arrayInt[arrayInt.length-1]);
System.out.println("最小值:" + arrayInt[0]);
System.out.println("平均值:" + avg);
System.out.println("所有元素之和:"+sumNumber);
}
public static void main(String[] args) {
getValue();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.*;
public class Test{
public static void main(String[] args) {
int [] nums = new int[20];
int max = 0;
int min = 0;
int total= 0;
double avg = 0;
Scanner input = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
System.out.print("请输入第" + (i+1)+"个数:");
nums[i] = input.nextInt();
}
for(int i=0;i<nums.length - 1;i++){
if (nums[i] > nums[i + 1]) {
max = nums[i];
}
if(nums[i] > nums[i + 1]){
min = nums[i+1];
}
total += nums[i];
}
total += nums[nums.length - 1];
avg = ((double)total)/nums.length;
System.out.println("最大值" + max);
System.out.println("最小值" + min);
System.out.println("总和:" + total);
System.out.println("平均值:" + avg);
}
}
public class Test{
public static void main(String[] args) {
int [] nums = new int[20];
int max = 0;
int min = 0;
int total= 0;
double avg = 0;
Scanner input = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
System.out.print("请输入第" + (i+1)+"个数:");
nums[i] = input.nextInt();
}
for(int i=0;i<nums.length - 1;i++){
if (nums[i] > nums[i + 1]) {
max = nums[i];
}
if(nums[i] > nums[i + 1]){
min = nums[i+1];
}
total += nums[i];
}
total += nums[nums.length - 1];
avg = ((double)total)/nums.length;
System.out.println("最大值" + max);
System.out.println("最小值" + min);
System.out.println("总和:" + total);
System.out.println("平均值:" + avg);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("请输入20个数:"+):
Scanner in = new Scanner(System.in);
int i = 0;
int input = 0;
int[] a = new int[20];
while (i < 20) {
a[i] = in.nextInt();
}
int max = Max(a);
int min = Min(a);
int sum = Sum(a);
int average = sum/20;
System.out.println("最大值:"+max);
System.out.println("最小值:"+min);
System.out.println("平均值:"+average);
System.out.println("总和:"+sum);
}
private static int Sum(int[] a) {
int sum = 0;
for(int i = 0;i<a.length;i++){
sum+=i;
}
return sum;
}
private static int Min(int[] a) {
int min = a[0];
for(int i = 1;i<a.length;i++){
if(a[i]>min){
min = a[i];
}
}
return min;
}
private static int Max(int[] a) {
int max = a[0];
for(int i = 1;i<a.length;i++){
if(a[i]>max){
max = a[i];
}
}
return max;
}
}
public class Test {
public static void main(String[] args) {
System.out.println("请输入20个数:"+):
Scanner in = new Scanner(System.in);
int i = 0;
int input = 0;
int[] a = new int[20];
while (i < 20) {
a[i] = in.nextInt();
}
int max = Max(a);
int min = Min(a);
int sum = Sum(a);
int average = sum/20;
System.out.println("最大值:"+max);
System.out.println("最小值:"+min);
System.out.println("平均值:"+average);
System.out.println("总和:"+sum);
}
private static int Sum(int[] a) {
int sum = 0;
for(int i = 0;i<a.length;i++){
sum+=i;
}
return sum;
}
private static int Min(int[] a) {
int min = a[0];
for(int i = 1;i<a.length;i++){
if(a[i]>min){
min = a[i];
}
}
return min;
}
private static int Max(int[] a) {
int max = a[0];
for(int i = 1;i<a.length;i++){
if(a[i]>max){
max = a[i];
}
}
return max;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Num {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int num[]=new int[20];
for(int i=0;i<20;i++){
num[i]=in.nextInt();
}
int max=0,min=0,sum=0;
max=min=sum=num[0];
for(int i=1;i<20;i++){
sum+=num[i];
if(num[i]>max){
max=num[i];
}
if(num[i]<min){
min=num[i];
}
}
System.out.println("max="+max+"\nmin="
+min+"\nsum="+sum+"\naverage="+(float)(1.0*sum/20));
}
}
未测试.
请自行测试。
public class Num {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int num[]=new int[20];
for(int i=0;i<20;i++){
num[i]=in.nextInt();
}
int max=0,min=0,sum=0;
max=min=sum=num[0];
for(int i=1;i<20;i++){
sum+=num[i];
if(num[i]>max){
max=num[i];
}
if(num[i]<min){
min=num[i];
}
}
System.out.println("max="+max+"\nmin="
+min+"\nsum="+sum+"\naverage="+(float)(1.0*sum/20));
}
}
未测试.
请自行测试。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询