Posted on 2010-06-26 09:02
saobaolu 閱讀(2934)
評論(0) 編輯 收藏 所屬分類:
java基礎與算法
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了哈
沒有所謂的命運,只有不同的選擇!