/*
* 要求用键盘随便输入5个数字,读出,并采用冒泡排序将数组由大到小排序
*/package shuzi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import arrexcise.SortArr;
public class NumTest
{public static void main(String[] args) throws IOException
{ ArrayList list=new ArrayList();//采用集合添加
int count=0;
//StringBuffer sb=new StringBuffer();
String arr1;
while(true)//键盘输入数字进行判断
{ InputStreamReader iin=new InputStreamReader(System.in);
BufferedReader bin=new BufferedReader(iin);
arr1=bin.readLine();
count++;
if(count==6) //条件判断控制输入的数字个数跳出循环
break;
int num=Integer.parseInt(arr1);
list.add(num);//将通过集合添加的方法添加的数字填入集合
}
int [] num=new int[ list.size()];//定义数组,将集合转为数组
for (int i = 0; i < list.size(); i++) {
num[i]=(int)list.get(i);//数组元素的赋值
}
System.out.println("输出原始数组为:");
System.out.print("[");//输出数组元素
for (int i = 0; i < num.length; i++) {
if(i<num.length-1)
{System.out.print(num[i]+" ");}
else
System.out.println(num[i]+" ]");
}
SortArr.sortArr(num);//调用冒泡排序进行排序
System.out.println("冒泡排序后输出数组为:");//输出数组
System.out.print("[");
for (int i = 0; i < num.length; i++) {
if(i<num.length-1)
{System.out.print(num[i]+" ");}
else
System.out.println(num[i]+" ]");
}
SortArr.selectArr(num);//调用选择排序进行排序
System.out.println("选择排序后输出数组为:");//输出数组
System.out.print("[");
for (int i = 0; i < num.length; i++) {
if(i<num.length-1)
{System.out.print(num[i]+" ");}
else
System.out.print(num[i]+" ]");
}
}
}
|
|