JJ プログラム仙人修行日誌

2024/04/20 からは、プログラム仙人修行の日誌を書いてます。

gg120.java

//実行にあたり,intsmp.txt ファイルを用意し
/* intsmp.txt の中は
52
31
100
7
50
*/

import java.io.*;

class gg120 {
    public static void main(String[] args) {

        int [] m =  new int[10];
        int n = 0;

        try{
            BufferedReader xin = new BufferedReader(new FileReader("intsmp.txt"));
            String xlinebuf;
            while( (xlinebuf = xin.readLine()) != null){
                int buf = Integer.parseInt(xlinebuf);
                System.out.println("読み取った数字は,"+ buf); // 読み取った数字を表示

                // 挿入する位置を決める
                int l = 0;
                // 教科書は,ループ終了条件であるのを,継続条件で書き換え
                while(( l < n )&&(buf < m[l])){
                    l++;
                }
                System.out.println("挿入位置は,"+ l);

                //後ろを移動
                int j = n;
                while(j>=l){ // 教科書の終了条件を継続条件に書き換え
                    m[j+1] = m[j];
                    j--;
                }
                m[l] = buf;
                n++;

                //現在までの整列済みを表示
                System.out.print("現在の整列済み配列は ");
                for(int i=0; i<n; i++){
                    System.out.print(m[i] + ", ");
                }
                System.out.println();System.out.println();
            }
            xin.close();
        } catch (IOException ie){
            System.out.println("ファイルがありません。");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}