2014-09-25 8 views
-5
내가 프로그래밍 초보자 오전

는 사람이어떻게 배열에서 배열을 반환합니까?

public int [][]matrixSetup(String size, char i) throws IOException { 

    int size_num = Integer.parseInt(size); 

    if (size_num > 1 && size_num < 4) { 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 


    System.out.println("input the value store them, they arrange as" + i + "11,"+ i+ "12," + i +"21,"+ i +"22 etc"); 

    for(int row = 0; row < size_num ; row++) 
     for(int col = 0 ; col < size_num ; col++){ 
      int [][]A = new int [size_num][size_num]; 
      String ipS = br.readLine();      //ipS = input String 
      int input_value = Integer.parseInt(ipS); 
      A[row][col] = input_value;   
     } 
    } else System.out.println("invalid matrix size!"); 
    return A; //How to return the matrix? 
} 
+1

그것의 각 반복에 대한 새로운 배열을 만들 –

+1

자바 언어. 루프 외부에 A를 작성해야합니다. – eternay

+2

[가변 범위] (http://www.java-made-easy.com/variable-scope.html)에 대해 알아보십시오. – GriffeyDog

답변

0

는 "어떻게 행렬을 반환하는"귀하의 질문에 대답하기 위해, 내 문제를 해결하시기 바랍니다 도움이 될 수 있기를 바랍니다 : 당신은 이미 올바른 방법으로하고 있습니다. 지금까지의 의견에서 알 수 있듯이, 문제는 A의 선언에 있습니다.

A 선언 내부 초기화된다 for -loop :

int [][]A = new int [size_num][size_num]; 

따라서 가시성이 for -loop 제한된다. 당신이 선언 이동해야하는 기존 returnA 볼 수 있도록하려면

int size_num = Integer.parseInt(size); 
int[][] A = new int[size_num][size_num]; 
if (size_num > 1 && size_num < 4) { 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    for(int row = 0; row < size_num ; row++) 
    for(int col = 0 ; col < size_num ; col++){ 
     String ipS = br.readLine(); 
     int input_value = Integer.parseInt(ipS); 
     A[row][col] = input_value; 
    } 
} else { 
    System.out.println("invalid matrix size!"); 
} 
return A; 
+0

감사합니다. Stefan Freitag –

관련 문제