/**
* @author HP-Developer
* 8-31号的数组练习
* 实验任务
实验任务
将数组中的一组数据,从小到大依次输出
实验要求
数组必须是数字类型
随机定义一组数组
*/
package testof8_31;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author HP-Developer
* 8-31号的数组练习
* 实验任务
实验任务
将数组中的一组数据,从小到大依次输出
实验要求
数组必须是数字类型
随机定义一组数组
*/
public class Test3_1{
public static void main(String[] args){
int [] num={2,3,456,78,32,6,8,4,87,345};
int temp;
for(int i=0;i<num.length;i++){
for(int j=0;j<num.length-1;j++){
if(num[j]>=num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
for(int i:num){
System.out.print(i+"\t");
}
System.out.print("\n");
int [] num1={2,3,456,78,32,6,8,4,87,345};
Arrays.sort(num1);
for(int i:num1){
System.out.print(i+"\t");
}
System.out.println("\n是否 转换顺序输出?(Y/N):");
Scanner input=new Scanner(System.in);
char choice=input.next().charAt(0);
switch(choice){
case 'Y':
System.out.println("大小反序排列为:");
for(int i=0;i<num.length;i++){
for(int j=0;j<num.length-1;j++){
if(num[j]<=num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
for(int i:num){
System.out.print(i+"\t");
}
break;
case 'N':
System.out.println("排序结束!");
break;
}
}
}
/*运行结果:
2 3 4 6 8 32 78 87 345 456
2 3 4 6 8 32 78 87 345 456
是否 转换顺序输出?(Y/N):
Y
大小反序排列为:
456 345 87 78 32 8 6 4 3 2
*
*/