//数组练习
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]) ;
}
}
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]) ;
}
}