java...从键盘上输入10个整数,并按升序排序后输出~~~
代码如下
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;
}
}
Scanner scanner=new Scanner(System.in);
int[] test = new int[10];
for (int i = 0; i < 10; i++) {
System.out.println("请输入地"+(i+1)+"个数");
int a=scanner.nextInt();
test[i]=a;
}
for (int i = 10; i > 0; i--) {
for (int j = 0; j < i-1; j++) {
if(test[j]>test[j+1]){
int temp = test[j];
test[j]= test[j+1];
test[j+1] = temp;
}
}
}
System.out.println("排序后数组");
for (int i = 0; i <10; i++) {
System.out.println(test[i]);
}
}
望采纳
非常感谢~~~再问一个...编写一个java Application 程序,接受用户输入的5个整数,并输出这5个整数的最大和最小值
用我刚才的代码。输出list中的第一个和最后一个就可以实现..
import java.util.Arrays;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
for(int i=0;i<10;i++){
System.out.println("请输入第"+i+"个数:");
a[i]=sc.nextInt();
}
Arrays.sort(a);
for(int j=0;j<10;j++){
System.out.print(a[j]+" ");
}
}
}