-1
import  javax.swing.*;
public class Main {

public static void main(String[] args) {
int r=0,c=0;

String input,inputt;
input = JOptionPane.showInputDialog("Plz Enter the number of Rows");
r = Integer.parseInt(input);

 input = JOptionPane.showInputDialog("Plz Enter the number of Coloms");
c = Integer.parseInt(input);

int array[][]= new int[r][c];
for (int i=0;i<=r;i++)
    {
        for (int j=0;j<=c;j++)
        input = JOptionPane.showInputDialog("Plz Enter the elemet of the array");
        array [r][c]= Integer.parseInt(input);
    }


}

我正在尝试使用 JOption 声明二维数组

4

2 回答 2

8

您正在使用for循环条件运行 2D 数组的末尾:

for (int i=0;i<=r;i++)
{
    for (int j=0;j<=c;j++)

有效的索引是0throughr - 10through c - 1,所以你对每一个都走得太远了。尝试

for (int i=0;i < r;i++)
{
    for (int j=0;j < c;j++)
于 2013-11-08T19:17:46.337 回答
2

您的代码中几乎没有错误

  1. 你从不检查用户输入的内容(如果它是“一个”)
  2. 你的循环将包括它们的最大索引r和范围cijr-1c-1
  3. for (int j=0;j<c;j++)读取和转换用户数据时需要在代码块中完成,否则循环将只执行读取c次数
  4. 在您的循环中,您使用的是固定索引array [r][c]而不是array [i][j].
于 2013-11-08T19:21:42.173 回答