posts - 89, comments - 241, trackbacks - 0, articles - 1
             :: 首頁 ::  :: 聯系 :: 聚合  :: 管理
           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 }
          最后應該把p給close了哈

          沒有所謂的命運,只有不同的選擇!
          主站蜘蛛池模板: 嵊州市| 磴口县| 常州市| 荃湾区| 白河县| 宜城市| 吐鲁番市| 玉山县| 沐川县| 酉阳| 奎屯市| 青浦区| 武义县| 长泰县| 郸城县| 凯里市| 巴楚县| 巴南区| 沾化县| 大田县| 商丘市| 抚顺市| 隆尧县| 南涧| 蕲春县| 永泰县| 盐池县| 桑植县| 秀山| 丰城市| 仪陇县| 阳原县| 普定县| 清苑县| 上犹县| 什邡市| 玉屏| 昌江| 孟村| 西青区| 介休市|