结构推理 编写一个程序,从键盘输入10个整数,并将这些数据排序后在标准输出上输出。
【正确答案】import java.io.*; public class Sort{ public static void main(String args[]) { int a[]=new int[10]; byte b[]=new byte[10]; int t; String str; System.out.println("请输入10个整数:"); try{ for(int i=0;i<10;i++) {System.out.print("No. "+(i+1)+": "); System.in.read(b); str=new String(b); str=str.trim(); a[i]=Integer.parseInt(str); } }catch(IOException e){ System.out.println(e.toString()); } catch(NumberFormatException e){ System.out.println(e.toString()); } for(int i=0;i<9;i++) for(int j=i+1;j<10;j++) { if(a[i]>a[j]){ t=a[i]; a[i]=a[j]; a[j]=t; } } for(int i=0;i<10;i++) System.out.println(a[i]+"/t"); } }
【答案解析】