数组练习

//数组练习
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class Test01
{
 public static void main(String[] args)
    {
  Scanner scan = new Scanner(System.in) ;
  double[] score = new double[5] ;
  for(int i = 0 ; i < score.length ; i ++)
  {
   System.out.println("第"+(i+1)+"次输入:") ;   
   score[i] = scan.nextInt() ;
  }
  //++++++++++++++++++++++++++++++++++++++++++++++++++++
  for(int i = 0 ; i < score.length ; i ++)  
  {
   System.out.print("第"+(i+1)+"次\t") ; 
  }
      System.out.println() ;                   //循环提示输入
  //++++++++++++++++++++++++++++++++++++++++++++++++++++
  for(int i = 0 ; i < score.length ; i ++) 
  {
   System.out.print(score[i]+"\t") ;
  }
  System.out.println() ;                       //遍历数组输出
  //++++++++++++++++++++++++++++++++++++++++++++++++++++
  double sum = 0 ;                         
  for(int i = 0 ; i < score.length ; i ++)
  {
            sum += score[i] ;  
  }                                            //求所有数的和
  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  double max = score[0] ;                    
  for(int i = 0 ; i < score.length ; i ++)
  {
            if(score[i]>max){max = score[i] ;
            }  
  }                                           //普通方式最大数
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                             
  int maxID = 0 ;                         
  for(int i = 1 ; i < score.length ; i ++)
  {
            if(score[i]>score[maxID]){maxID = i ;}  
  }                                           //用序号找 最大数
  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  System.out.println("-------------------------------------") ;
  System.out.println("经过系统排序,从小到大,结果如下:") ;                                     
  int tem = 0 ; //最大值序号
  for(int q = 0 ; q < score.length-1 ;q ++)
  {
   tem = q ;                               
   for(int j = q+1 ; j < score.length; j++)
   {
    if(score[j]<score[tem])
    {
     tem = j ;
    }
   }
   if(q != tem)
   {
    double a = score[q] ;
    score[q] = score[tem] ;
    score[tem] = a ;
   }
  }
  for(int q = 0 ; q < score.length ; q ++)  //遍历数组输出
  {
   System.out.print(score[q]+"\t") ;
  }
  System.out.println() ;
  
  
  
  System.out.println("-------------------------------------") ;
  System.out.println("经过系统排序,从大到小,结果如下:") ;                                     
  int temp = 0 ; //最大值序号
  for(int i = 0 ; i < score.length-1 ;i ++)
  {
   temp = i ;                               
   for(int j = i+1 ; j < score.length; j++)
   {
    if(score[j]>score[temp])
    {
     temp = j ;
    }
   }
   if(i != temp)
   {
    double a = score[i] ;
    score[i] = score[temp] ;
    score[temp] = a ;
   }
  }
  for(int i = 0 ; i < score.length ; i ++)  //遍历数组输出
  {
   System.out.print(score[i]+"\t") ;
  }
  System.out.println() ;
  System.out.println("-------------------------------------") ;
                                                        //交换排序法
  //++++++++++++++++++++++++++++++++++++++++++++++++++++
  
  System.out.println("循序已经打乱,结果如下:") ;
  for(int i = 0 ; i < score.length-1 ; i ++)
  {
   int p = (new Random()).nextInt(score.length-1)+1;
   double a = score[i] ;
   score[i] = score[p] ;
   score[p] = a ;
  }
  for(int i = 0 ; i < score.length ; i ++)  //遍历数组输出
  {
   System.out.print(score[i]+"\t") ;
  }
  System.out.println() ;
  System.out.println("-------------------------------------") ;
                                                //洗牌算法
  //++++++++++++++++++++++++++++++++++++++++++++++++++++++
  System.out.println("和是"+sum) ;
  System.out.println("平均数是"+(sum/score.length)) ;
  System.out.println("最大数是"+max) ;
  System.out.println("序号排列最大数是"+score[maxID]) ;
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值