提问人:rhucha 提问时间:9/20/2022 最后编辑:Federico klez Cullocarhucha 更新时间:9/20/2022 访问量:34
我已经在 Java 中制作了一个程序来接受和显示矩阵,但我得到一个 ArrayIndexOutOfBoundsException [重复]
I have made a program in Java to accept and display a matrix but I am getting an ArrayIndexOutOfBoundsException [duplicate]
问:
当我运行程序时,accept method() 运行平稳,但是当我尝试运行 display() 方法时,它显示一个 .我认为这是因为存储在矩阵中的值没有传递给方法。我能做些什么?ArrayIndexOutOfBoundException: Index 0 out of bounds for length 0
m
display()
package matrices;
import java.util.Scanner;
class Functions {
int r,c,i,j,row,col;
Scanner in = new Scanner(System.in);
int m[][] = new int[r][c];
void accept() {
System.out.print("Enter no. of rows in the matrix : ");
r = in.nextInt();
System.out.print("Enter no. of columns in the matrix : ");
c = in.nextInt();
int m[][] = new int[r][c];
System.out.print("Enter the elements : ");
for (i=0; i<r; i++) {
for (j=0; j<c; j++) {
m[i][j] = in.nextInt();
}
}
}
void display() {
for (int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
System.out.print(m[i][j] + " ");
}
System.out.print("\n");
}
}
}
public class Matrix {
public static void main(String[] args) {
int ch;
Functions mat = new Functions();
do {
System.out.println("Menu\n1.Accept\n2.Display");
Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice : ");
ch = sc.nextInt();
switch(ch) {
case 1: mat.accept();
break;
case 2: mat.display();
break;
}
}while(ch<3);
}
}
答: 暂无答案
评论