gpt4 book ai didi

java - 我想使2D随机数数组出现错误: incompatible types: int cannot be converted to int[]

转载 作者:行者123 更新时间:2023-12-02 11:14:31 25 4
gpt4 key购买 nike

import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) {
int[][] randomNumbers = new int[10][];
Random rand = new Random();
System.out.print("Array1: [ ");
for (int i = 0; i < randomNumbers.length; i++) {
int n = rand.nextInt(20) + 0;
randomNumbers[i] = n;
}
for (int i = 0; i < randomNumbers.length; i++) {
System.out.print(" " + randomNumbers[i]) ;
}
System.out.print(" ]") ;
}
}

最佳答案

这是你想要的:

代码:带有随机数Run的2D数组

import java.util.Random;

public class Array2D_random {
public static void main(String[] args) {
int[][] randomNumbers = new int[10][10];

Random rand = new Random();

System.out.println("Array1: \n[ ");

// how to initialize two dimensional array in Java
// using for loop

for (int i = 0; i < randomNumbers.length; i++) { // loop for row
for (int j = 0; j < randomNumbers[i].length; j++) { // loop for column
int n = rand.nextInt(20) + 0;
randomNumbers[i][j] = n;
}
}
for (int i = 0; i < randomNumbers.length; i++) {
System.out.print(" {");
for (int j = 0; j < randomNumbers[i].length; j++) {
System.out.print(" " + randomNumbers[i][j] + " ");
}
System.out.print(" } ");
System.out.println();
}
System.out.print(" ]");
}
}

关于java - 我想使2D随机数数组出现错误: incompatible types: int cannot be converted to int[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51219541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com