代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TenNum {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("请输入10个数字用逗号隔开:");
BufferedReader bReader =new BufferedReader( new InputStreamReader(System.in));
String lineString = bReader.readLine();
if (lineString!=null) {
String e[] =lineString.split(",");;
System.out.println("排序前的数组:"+scanArray(e));
System.out.println("排序后的数组:"+scanArray(selectionSort(e)));
}
}
public static String[] selectionSort(String[] elements) {
for (int i = 0; i < elements.length - 1; ++i) {
int k = i;
for (int j = i; j < elements.length; ++j) {
if (Integer.parseInt(elements[k]) > Integer.parseInt(elements[j])) {
k = j;
}
}
if (k != i) {// 交换元素
String temp = elements[i];
elements[i] = elements[k];
elements[k] = temp;
}
}
return elements;
}
public static String scanArray(String[] elements) {
String str = "";
for (int j = 0; j < elements.length; j++) {
str = str + " " + elements[j];
}
return str;
}
}
非常感谢~~~再问一个...编写一个java Application 程序,接受用户输入的5个整数,并输出这5个整数的最大和最小值
追答用我刚才的代码。输出list中的第一个和最后一个就可以实现..
追问呃呃 不会啊~~~汗...