【问题标题】:Error:(17, 34) java: incompatible types: int cannot be converted to int[] [duplicate]错误:(17、34)java:不兼容的类型:int 无法转换为 int [] [重复]
【发布时间】:2018-02-07 15:07:55
【问题描述】:

我刚开始学习这个,不知道为什么会出现这个问题:

“错误:(17, 34) java: 不兼容的类型: int 无法转换为int[]”帮助!

public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);
        System.out.println("Vvedite razmer massiva (2 chisla");
        int size1 = sc.nextInt();
        int size2 = sc.nextInt();
        int [] [] array = new int  [size1] [size2];
        System.out.println("Vvedite chisla massiva");
        for (int i=0; i< size1*size2; i++) {
            array[i] = sc.nextInt();
        }
        System.out.println("Vash massiv:");

        for (int i=0; i< size1*size2; i++){
            System.out.print (array [i] + "\t");
        }
        System.out.println();


        }
    }

【问题讨论】:

标签: java


【解决方案1】:

您创建了一个二维数组,但您试图仅使用一维来填充它

for (int i=0; i< size1; i++) {
        for (int j=0;j<size2;j++)
        array[i][j] = sc.nextInt();
    }

显示数组也是如此

【讨论】:

  • 很高兴能帮到你,如果我的回答对你有帮助,你可以采纳,或者+1
【解决方案2】:

array[][] 视为数组数组。所以,array[i] 有一个 int 数组,而不是 int(例如,int 位于 array[i][j]

【讨论】:

  • 非常感谢!我现在很清楚:)
  • @ІринаЧистякова 很高兴能帮到你:-)
猜你喜欢
  • 2016-09-05
  • 2017-10-03
  • 2023-01-18
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
相关资源
最近更新 更多