我是编程的初学者,希望有人能帮我解决我的问题,请
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?
}
答案 0 :(得分:0)
回答你的问题&#34;如何返回矩阵&#34;:你已经以正确的方式做了。从目前为止的评论中可以看出,问题在于A
。
A
在for
- 循环中声明并初始化:
int [][]A = new int [size_num][size_num];
因此,其可见性仅限于for
- 循环。要使A
对现有的return
语句可见,您必须移动其声明:
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;