Non-Zero Two Dim Array


Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type
Allowed languages
Java 19, Java 8

這個程式允許使用者輸入一個整數二維陣列,首先輸入有幾個橫列,再輸入有幾個直行,最後輸入對應陣列尺寸的每個元素(從左到右,從上到下)。 設定每個元素內容的時候,會檢查數值的合法性,若數值小於等於0,則忽略它,不設定該數值。

輸入完成之後,程式會輸出陣列的所有數值總和。

這個程式目前仍缺少NonZeroTwoDimArray類別的實作,請完成它,並且上傳完整的程式。 除了NonZeroTwoDimArray類別之外,請勿修改其他已經寫好的部分,違反此規則者,無論自動評測結果為何,在考試中均不計分。但允許輕微的排版差異。

程式完成之後,可用以下測試資料做初步的測試,但仍請自行設計完整的測試資料

輸入 #1:

3 3 1 2 3 4 5 6 7 8 9

輸出 #1:

45

輸入 #2:

3 3 1 -1 -1 -1 1 -1 -1 -1 1

輸出 #2:

3

import java.util.Scanner;

// 定義NonZeroTwoDimArray類別,內部有一個二維整數陣列,表示二維陣列的元素,每個元素的預設值為 0
// 建構函數的第一個參數是整數 nRows,第二個參數是整數 nCols。
// NonZeroTwoDimArray類別有一個setElement方法,接受三個參數,分別是整數 row、整數 col 和整數 value,沒有返回值,
// 如果 value 大於 0,則將 value 設置到二維陣列的 row 行 col 列。
// NonZeroTwoDimArray類別有一個getSum方法,返回整數,表示二維陣列中所有元素的和。
class NonZeroTwoDimArray {
    ...
}

public class TwoDimArrayClass {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 讀取兩個整數,分別表示二維陣列的行數和列數
        int nRows = scanner.nextInt();
        int nCols = scanner.nextInt();

        // 創建一個二維陣列,行數為 nRows,列數為 nCols
        NonZeroTwoDimArray array = new NonZeroTwoDimArray(nRows, nCols);

        // 接著讀取 nRows * nCols 個整數,並將它們存儲在二維陣列中
        for (int i = 0; i < nRows; i++) {
            for (int j = 0; j < nCols; j++) {
                int value = scanner.nextInt();

                // 設置二維陣列中的元素,第一個參數是行,第二個參數是列,第三個參數是值
                // 如果值大於 0,則設置到二維陣列中,否則忽略
                array.setElement(i, j, value);
            }
        }

        // 輸出二維陣列中所有元素的和
        System.out.println(array.getSum());

        scanner.close();
    }
}

Comments


  • 0
    scu09156146  commented on April 26, 2024, 2:57 p.m.

    題解

    import java.util.Scanner;
    
    // 定義NonZeroTwoDimArray類別
    class NonZeroTwoDimArray {
        int nRows;
        int nCols;
        int[][] array; // 每個元素的預設值為0
    
        // 建構函數的第一個參數是整數 nRows,第二個參數是整數 nCols
        public NonZeroTwoDimArray(int nRows, int nCols) {
            this.nRows = nRows;
            this.nCols = nCols;
            this.array = new int[nRows][nCols];
        }
    
        // setElement():接三個參數(row、col、value),沒有返回值
        public void setElement(int i, int j, int value) {
            // 如果value大於0,將value設置到陣列,否則忽略
            if (value > 0) {
                this.array[i][j] = value;
            }
        }
    
        // getSum():返回二維陣列中所有元素的和
        public int getSum() {
            int sum = 0;
            for (int i = 0; i < nRows; i++) {
                for (int j = 0; j < nCols; j++) {
                    sum += this.array[i][j];
                }
            }
            return sum;
        }
    }
    
    public class TwoDimArrayClass {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            // 讀取兩個整數,分別表示二維陣列的行數和列數
            int nRows = scanner.nextInt();
            int nCols = scanner.nextInt();
    
            // 創建一個二維陣列,行數為 nRows,列數為 nCols
            NonZeroTwoDimArray array = new NonZeroTwoDimArray(nRows, nCols);
    
            // 接著讀取 nRows * nCols 個整數,並將它們存儲在二維陣列中
            for (int i = 0; i < nRows; i++) {
                for (int j = 0; j < nCols; j++) {
                    int value = scanner.nextInt();
    
                    // 設置二維陣列的元素,第一個參數是行,第二個參數是列,第三個參數是值
                    array.setElement(i, j, value);
                }
            }
    
            // 輸出二維陣列中所有元素的和
            System.out.println(array.getSum());
    
            scanner.close();
        }
    }