Java作業:求解1到100之間的偶數

    // 3.求解1到100之間的偶數
    public static void Test3() {
        System.out.println("1到100之間的偶數爲:");
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.print(i + " ");
            }
        }
    }

這裏寫圖片描述
這裏寫圖片描述



這樣看着不是不方便,稍微改動下,每行少輸出幾個數字,多輸出幾行

// 3.求解1到100之間的偶數(輸入是,每行輸出9個數字)
    public static void Test3_3() {
        int count = 0;
        System.out.println("1到100之間的偶數爲:");
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.print(i + "   ");
                count++;
                if (count == 9) {
                    System.out.print("\n----------------------------------------\n");
                    count = 0;
                }
            }
        }
    }

這裏寫圖片描述

發佈了57 篇原創文章 · 獲贊 67 · 訪問量 43萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章