今日のサンプル

(for id:yurli)

public class sample1 {

    public static void main(String[] args) throws Exception {
        int[] i = new int[10];
        //while文を使ってデータを挿入
        int j = 0;
        while (j < 10) {
            i[j] = j * 2;
            j++;
        }
        int total = 0;
        j = 0;
        //while文を使って配列に入っている値の和を求めます
        while (j < 10) {
            total = total + i[j];
            j++;
        }
        j = 0;
        while (j < 10) {
            System.out.print("i[");
            System.out.print(j);
            System.out.print("]=");
            System.out.println(i[j]);
            j++;
        }
        //最後に結果を表示します
        System.out.print("合計=");
        System.out.println(total);

    }
}

for文をwhile文で書き直しただけ。最近、Java入門者講座でもやっているみたい。実際内容がそうだけども。これくらいのコードは10分くらいでかけるようになってもらいたいものだ。