需要Java数组帮助使用scanner类来输出平均值和排序方法

几个小时后,我没有在制作输出平均值的方法上取得进展.我还需要编写一个排序类.总的来说,任务需要.

制定方法:

>’main’方法打印数组
>对数组进行排序
>确定最高值
>确定最低值
>计算平均值(双倍)

import java.util.Scanner;

public class Arrayassignment {

public static void main(String[] args) {
   Scanner sin=new Scanner(System.in);

System.out.println("Enter an intiger for array size.");

int number= sin.nextInt();

int array[]=new int[number];

System.out.println("Array size " + number + " initiated.\n");


System.out.println("Now enter the array intigers");
for (int i = 0; i < number; i++)
{  
    array[i]=sin.nextInt();

 }

System.out.println ("The highest number in the array is " + max (array) );
System.out.println ("The smallest number in the array is " + min (array) );
//System.out.println ("The average of the numbers in the array is " ;
}
public static int max ( int ... arg )
{
 if ( arg.length == 0) 
 { 
   System.out.println(" empty arguement list ")  ;
   return 0;
 }
 int largest = arg [0];
 for (int i = 1; i < arg.length; i++ )
 {
   if (arg [i] > largest)
       largest = arg[i];
 }
 return largest;
 }

 public static int min ( int ... arg )
 {
 if ( arg.length == 0) 
 { 
   System.out.println(" empty arguement list ")  ;
   return 0;
 }
 int smallest = arg [0];
 for (int i = 1; i < arg.length; i++ )
 {
   if (arg [i] < smallest)
       smallest = arg[i];
 }
 return smallest;
最佳答案
int sum = 0;
for (int d : arg ) sum += d;
double average = 1.0d * sum / arg .length;

这将帮助您找到平均值…

转载注明原文:需要Java数组帮助使用scanner类来输出平均值和排序方法 - 代码日志