键盘输入10个整数,冒泡排序并输出结果到文件

1  import  java.io. * ;
 2  import  java.util.Scanner;
 3  public   class  SortTest {
 4       public   static   void  main(String[] args)  throws  IOException {
 5           int  temp  =   0 ; // 临时变量,用于冒泡交换        
 6           int [] num = new   int [ 10 ];   // 声明一个空的数组  10个长度
 7          Scanner sc  =   new  Scanner(System.in);
 8          FileOutputStream out = new  FileOutputStream( " 1.txt " );
 9          PrintStream p = new  PrintStream(out);
10           // 开始循环赋值
11           for ( int  i  = 0 ;i < num.length;i ++ ){
12              num[i] = sc.nextInt();
13          }
14          p.append( " 排序前为: " );
15           for  ( int  i  =   0 ; i  < num.length; i ++ ) {
16              System.out.println(num[i]);
17              p.append(num[i] + "  ,  " );
18          }
19           //  用于排序
20           for  ( int  i  =   0 ; i  <  num.length - 1 ; i ++ ) {
21               for  ( int  j  =   0 ; j  <  num.length  -  i  -   1 ; j ++ ) {
22                   if  (num[j]  >  num[j  +   1 ]) {
23                      temp  =  num[j];
24                      num[j]  =  num[j  +   1 ];
25                      num[j  +   1 =  temp;
26                  }
27              }
28          }
29           // 输出文件
30          p.append( " \n " );
31          p.append( " 排序后为: " );
32           //  循环输出
33          System.out.println( " 排序后为: " );
34           for  ( int  i  =   0 ; i  < num.length; i ++ ) {
35              System.out.println(num[i]);
36              p.append(num[i] + "  ,  " );
37          }
38      
39          
40      }
41  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值