所以我有一个问题,Java告诉我,以下行是表达式的非法开始,而不是语句和';'预计,等...在定义数组值时不能使用变量?
表达式为:
Random rand = new Random();
int[][] coords = new int[24][2];
start = rand.nextInt(16);
coords[0][0]={0,start};
什么是错的表达?
所以我有一个问题,Java告诉我,以下行是表达式的非法开始,而不是语句和';'预计,等...在定义数组值时不能使用变量?
表达式为:
Random rand = new Random();
int[][] coords = new int[24][2];
start = rand.nextInt(16);
coords[0][0]={0,start};
什么是错的表达?
在coords[0][0]={0,start};
,{0, start}
根本不是一个有效的表达式。它看起来像你试图初始化数组的第一行,在这种情况下,你要找的东西沿着这些路线:
Random rand = new Random();
int[][] coords = new int[24][2];
start = rand.nextInt(16);
coords[0][0] = 0;
coords[0][1] = start;
哇......我忘记了定义数组的最重要规则之一......我应该多研究一下它们。 – jocopa3 2012-03-29 03:36:10
那条规则是...? – 2012-03-29 03:36:36
我相信
Random rand = new Random();
int[][] coords = new int[24][2];
coords[0] = new int[] {0, rand.nextInt(16)};
也有效。这更符合你所尝试的精神,但这与上述答案没有多大区别。
你究竟想在最后一行分配什么? – 2012-03-29 03:34:26