2017-01-19 4 views
2

ユーザー入力から2D配列を作成し、配列を印刷するプログラムを実行しようとしています。配列は1つのメソッドで構築し、別のメソッドで行を出力する必要があります。私は新しい行の配列から各行を印刷することを探しています。あるメソッドから別のメソッドに配列を引き出す方法がわかりません。Javaがmain関数からprintメソッドを呼び出し、別の別のメソッドのデータを使用する

mainメソッドが呼び出されたときにコードが設定されるので、配列を構築するために "コンストラクタ"メソッドが呼び出され、配列が構築された後、その配列を受け取り、データをprintメソッド各行を印刷します。コード内のどこから進んでいくのかは分かりません。さまざまな方法からデータを引き出す方法はまだ分かりません。

package workfiles; 

import java.util.*; 
import java.util.Scanner; 

public class hw2 
{ 
    // Do not modify this method 
    public static void main(String[] args) 
    { 
     try { 
      int[][] iArray = enter2DPosArray(); 
      System.out.println("The original array values:"); 
      print2DIArray(iArray); 

      int[][] tArray = transposition(iArray); 
      System.out.println("The transposed array values:"); 
      print2DIArray(tArray); 
     } catch (InputMismatchException exception) { 
      System.out.println("The array entry failed. The program will now halt."); 
     } 
    } 

    // A function that prints a 2D integer array to standard output 
    // THIS METHOD IS THE ONE THAT IS SUPOSED TO PRINT THE ROWS ON NEW LINES 
    public static void print2DIArray(int[][] output) 
    { 
     int[][] iArray; 

     for (int row = 0; row < iArray.length; row++) { 
      for (int column = 0; column < iArray[row].length; column++) { 
       System.out.print(iArray[row][column] + " "); 
      } 
      System.out.println(); 
     } 
    } 

    // A function that enters a 2D integer array from the user 
    // It raises an InputMismatchException if the user enters anything other 
    // than positive (> 0) values for the number of rows, the number of 
    // columns, or any array entry 
    public static int[][] enter2DPosArray() throws InputMismatchException 
    { 
     int row = 0, col = 0, arow = 0, acol = 0, holder; 

     Scanner numScan = new Scanner(System.in); 

     while (row <= 0) { 
      System.out.print("How many rows (>0) should the array have? "); 
      row = numScan.nextInt(); 
     } 

     while (col <= 0) { 
      System.out.print("How many columns (>0) should the array have? "); 
      col = numScan.nextInt(); 
     } 

     int[][] iArray = new int[row][col]; 

     while (arow < row) { 
      while (acol < col) { 
       System.out.println("Enter a positive (> 0) integer value: "); 
       holder = numScan.nextInt(); 
       iArray[arow][acol] = holder; 
       acol++; 
      } 

      if (acol >= col) { 
       acol = 0; 
       arow ++; 
      } 
     } 

     //arrayName[i][j] 
     numScan.close(); 
     return iArray; 
    } 

    public static int[][] transposition(int[][] iArray) 
    { 
     int r = 0, c = 0; 

     int[][] transpose = new int[r][c]; 
     for (int i = 0; i < r; i++) { 
      for (int j = 0; j < c; j++) { 
       transpose[i][j] = iArray[j][i]; 
      } 
     } 
     return transpose; 
    } 
} 

答えて

4
public static void print2DIArray(int[][] output) {  
    int[][] iArray; 
    for (int row = 0; row < iArray.length; row++) { 

あなたはprint2DIArrayメソッドを呼び出すと、作成したばかりの空の配列を反復処理しようとしています。私はあなたが渡された「出力」パラメータを反復処理したいと思います

+0

ありがとう!どのように私はそれを逃したかわからない私はすることができますときにこの答えをマーク –

+0

心配しないでください。ハッピーjavaing – Christy

2

あなたはちょうどこのようなあなたのメソッドで名前を変更することができます。

print2DIArray(int[][] iArray) 

だからあなたのコードは、この

//your array can send like an attribute, you can use it like is it 
public static void print2DIArray(int[][] iArray) { 
    //int[][] iArray you dont need to use another array here 

    for (int row = 0; row < iArray.length; row++) { 
    //... 
のようになります。
1

このようにコードを編集しました。私はそれが役に立つと願っています。

public class Test { 

    private static int[][] iArray ; 

    public static void main(String[] args) { 


    try 
    { 
     int [][] iArray = enter2DPosArray(); 
     System.out.println("The original array values:"); 
     print2DIArray(iArray); 
     int [][] tArray = transposition(iArray); 
     System.out.println("The transposed array values:"); 
     print2DIArray(tArray); 
    } 

    catch (InputMismatchException exception) 
    { 
     System.out.println("The array entry failed. The program will now halt."); 
    } 

} 

    // A function that prints a 2D integer array to standard output 
    // THIS METHOD IS THE ONE THAT IS SUPPOSED TO PRINT THE ROWS ON NEW LINES 

    public static void print2DIArray(int[][] output) { 
     //int iArray[][] = new int[0][]; 

     for (int row = 0; row < iArray.length; row++) { 
      for (int column = 0; column < iArray[row].length; column++) { 
       System.out.print(iArray[row][column] + " "); 
      } 
      System.out.println(); 
     } 
    } 



    // A function that enters a 2D integer array from the user 
// It raises an InputMismatchException if the user enters anything other 
// than positive (> 0) values for the number of rows, the number of 
// columns, or any array entry 
    public static int[][] enter2DPosArray() throws InputMismatchException { 

     int row=0; 
     int col=0; 
     int arow=0; 
     int acol=0; 
     int holder; 
     Scanner numScan = new Scanner(System.in); 

     while (row<=0){ 
      System.out.print("How many rows (>0) should the array have? "); 
      row = numScan.nextInt(); 
     } 

     while (col<=0){ 
      System.out.print("How many columns (>0) should the array have? "); 
      col = numScan.nextInt(); 
     } 
     iArray = new int[row][col]; 

     while (arow < row) { 

      while (acol < col) { 
       System.out.println("Enter a positive (> 0) integer value: "); 
       holder = numScan.nextInt(); 
       iArray[arow][acol] = holder; 
       acol++; 
      } 

      if (acol >= col) { 
       acol = 0; 
       arow ++; 

      } 

     } 
     //arrayName[i][j] 
     numScan.close(); 
     return iArray; 
    } 

    public static int[][] transposition(int [][] iArray) { 

     int r=0, c=0; 

     int[][] transpose = new int[r][c]; 
     for (int i = 0; i < r; i++) { 
      for (int j = 0; j < c; j++) { 
       transpose[i][j] = iArray[j][i]; 
      } 
     } 
     return transpose; 
    } 

} 
関連する問題