2014-12-01 70 views
0

我有一個任務來設計和實現處理2D陣列的方法。2D陣列方法和演示

它需要有具有以下靜態方法的一個實現類(Array2DMethods): readInputs()讀取然後行和列來回用戶讀取數對應的條目,以該大小。例如:如果用戶輸入3作爲行數,3輸入列數,它將聲明一個數組10並讀取9個條目。

max(int [][] anArray)它在2D參數陣列中返回最大值anArray

rowSum(int[][] anArray)它返回在一行中的元件的總和X anArray

columnSum(int[][] anArray)它返回元件在anArray的x列的總和**小心瓦特/不同長度的行如果

isSquare(int[][] anArray)檢查陣列是正方形(意味着每行具有相同的長度anArray本身)

displayOutputs(int[][] anArray)顯示2個Dim陣列元素

它還需要測試方法的測試類(Arrays2DDemo)。

我評論過我有問題的部件。我不知道如何測試readInputs方法以外的方法,也不知道如何格式化要求用戶輸入每行數字的部分。

這裏是我到目前爲止的代碼:

import java.util.Scanner; 

class Array2DMethods { 

    public static int [][] readInputs(){ 
     Scanner keyboard = new Scanner(System.in); 

     System.out.print("  How many rows? "); 
     int rows = keyboard.nextInt(); 

     System.out.print("  How many columns? "); 
     int columns = keyboard.nextInt(); 

     int [][] ret = new int[rows][columns]; 

     for (int i = 0; i<ret.length; i++) { 
      for (int j = 0; j < ret[i].length; j++) { 
       System.out.print("please enter an integer: "); //Need to format like Enter [0][0]: ... Enter [0][1]: ...etc. 
       ret[i][j] = keyboard.nextInt(); 
      } 
     } 

     return ret; 
    } 

    public static int max(int [][] anArray) { 
     int ret = Integer.MIN_VALUE; 

     for (int i = 0; i < anArray.length; i++) { 
      for (int j = 0; j < anArray[i].length; j++) { 
       if (anArray[i][j] > ret) { 
        ret = anArray[i][j]; 
       } 
      } 
     } 

     return ret; 
    } 

    public static void rowSum(int[][]anArray) { 
     int ret = 0; 

     for (int i = 0; i<anArray.length; i++) { 
      for (int j = 0; j < anArray[i].length; j++) { 
       ret = ret + anArray[i][j]; 
      } 
     } 
    } 

    public static void columnSum(int[][]anArray) { 
     int ret = 0; 

     for (int i = 0; i < anArray.length; i++) { 
      for (int j = 0; j < anArray[i].length; j++) { 
       ret = ret + anArray[i][j]; 
      } 
     } 
    } 

    public static boolean isSquare(int[][]anArray) { 

     for (int i = 0, l = anArray.length; i < l; i++) { 
      if (anArray[i].length != l) { 
       return false; 
      } 
     } 
     return true; 
    } 

    public static void displayOutputs(int[][]anArray) { 
     System.out.println("Here is your 2Dim Array:"); 
     for(int i=0; i<anArray.length; i++) { 
      for(int j=0; j<anArray[i].length; j++) { 
       System.out.print(anArray[i][j]); 
       System.out.print(", "); 
      } 
      System.out.println(); 
     } 
    } 
} 

類Arrays2DDemo:

public class Arrays2DDemo { 

    public static void main(String[] args){ 

     System.out.println("Let's create a 2Dim Array!"); 

     int [][] anArray = Array2DMethods.readInputs(); 

     Array2DMethods.max(anArray); 

     Array2DMethods.rowSum(anArray); 
     //need to print out and format like this: Ex Sum of row 1 = 60 ...etc 
     Array2DMethods.columnSum(anArray); 
     //need to print out and format like this: Ex Sum of column 1 = 60 ...etc. 
     Array2DMethods.isSquare(anArray); 
     //need to print out is this a square array? true 
     Array2DMethods.displayOutputs(anArray); 
     //need it to be formatted like [10, 20, 30] etc 
    } 
} 

回答

2

假設你想anArray是您在輸入過程中讀取的數組,你應該命名變量,因爲這樣的...

public static void main(String[] args){ 

    System.out.println("Let's create a 2Dim Array!"); 

    int[][] anArray = Array2DMethods.readInputs(); 

    System.out.println("max " + Array2DMethods.max(anArray)); 

    Array2DMethods.rowSum(anArray); 
    Array2DMethods.columnSum(anArray); 

    System.out.println("Square " + Array2DMethods.isSquare(anArray)); 

    Array2DMethods.displayOutputs(anArray); 

} 

假設你有一個功能f它只需要一個輸入x。問題是你要求計算機評估f(x)而不告訴它x是什麼。如果給x一個值,但是,例如x = 3,那麼詢問f(x)就變成合法的,因爲它變成了f(3),這可以被評估。