2016-04-01 89 views
0

对于我的代码的这个阶段,我试图从1-8范围随机打印一个8x8的数字网格。我正在努力将fill arraylist中的值赋给两个for循环中的随机数。我该如何解决这个问题。将for循环的值分配到整数数组列表中

这里是我的代码:

import java.util.*; // for Arraylist 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collection; 
import java.util.List; 
public class Game { 
static Scanner in = new Scanner(System.in); 
     int a1, a2, a3, a4, a5, a6, a7, a8; 
     int b1 ,b2 ,b3 ,b4 ,b5 ,b6 ,b7 ,b8; 
     int c1 ,c2 ,c3 ,c4 ,c5 ,c6 ,c7 ,c8; 
     int d1 ,d2 ,d3 ,d4 ,d5 ,d6 ,d7 ,d8; 
     int e1 ,e2 ,e3 ,e4 ,e5 ,e6 ,e7 ,e8; 
     int f1 ,f2 ,f3 ,f4 ,f5 ,f6 ,f7 ,f8; 
     int g1 ,g2 ,g3 ,g4 ,g5 ,g6 ,g7 ,g8; 
     int h1 ,h2 ,h3 ,h4 ,h5 ,h6 ,h7 ,h8; 
    public int fill(ArrayList<Integer>ar) { 
     gui(); 
     int[] space = new int[] {a1, a2 ,a3 ,a4 ,a5 ,a6 ,a7 ,a8 , 
            b1 ,b2 ,b3 ,b4 ,b5 ,b6 ,b7 ,b8 , 
            c1 ,c2 ,c3 ,c4 ,c5 ,c6 ,c7 ,c8 , 
            d1 ,d2 ,d3 ,d4 ,d5 ,d6 ,d7 ,d8 , 
            e1 ,e2 ,e3 ,e4 ,e5 ,e6 ,e7 ,e8 , 
            f1 ,f2 ,f3 ,f4 ,f5 ,f6 ,f7 ,f8 , 
            g1 ,g2 ,g3 ,g4 ,g5 ,g6 ,g7 ,g8 , 
            h1 ,h2 ,h3 ,h4 ,h5 ,h6 ,h7 ,h8 }; } 
public void gui() 
{System.out.println(a1+" "+a2+" "+a3+" "+a4+" "+a5+" "+a6+" "+a7+" "+a8); 
    System.out.println(b1+" "+b2+" "+b3+" "+b4+" "+b5+" "+b6+" "+b7+" "+b8); 
    System.out.println(c1+" "+c2+" "+c3+" "+c4+" "+c5+" "+c6+" "+c7+" "+c8); 
    System.out.println(d1+" "+d2+" "+d3+" "+d4+" "+d5+" "+d6+" "+d7+" "+d8); 
    System.out.println(e1+" "+e2+" "+e3+" "+e4+" "+e5+" "+e6+" "+e7+" "+e8); 
    System.out.println(f1+" "+f2+" "+f3+" "+f4+" "+f5+" "+f6+" "+f7+" "+f8); 
    System.out.println(g1+" "+g2+" "+g3+" "+g4+" "+g5+" "+g6+" "+g7+" "+g8); 
    System.out.println(h1+" "+h2+" "+h3+" "+h4+" "+h5+" "+h6+" "+h7+" "+h8); 
} 
public double num(ArrayList<String>ar){ 
int[] randomNumbers = new int[64]; 
for (int index = 0; index < ar.size(); index++) 
    { 
     randomNumbers[ index ] = (int) (Math.random()*8); 
     } 
     for (int index = 0; index < ar.size(); index++) 
    { 
     randomNumbers[ index ] = (int) (Math.random()*8); 
     randomNumbers[ index ] = system.out.println(space[index++]); 
     } 
    } 
    } 
+1

你已经在'fill'中声明了'fill'数组作为局部变量 - 所以它的作用域限于那个方法。也许你打算把它宣布为一个领域? (顺便说一下,我不认为声明64个单独的变量是一个好主意......我建议*只是*有一个矩阵......) –

+1

此代码不能编译。你在使用IDE吗? –

+0

是的,代码不会编译循环的第二个结尾。 –

回答

0

它看起来像你的INT []空间阵列个够方法内声明,因此不在范围内该方法之外。

如果你希望它在范围内,可以考虑将int []空间声明为你的类的一个字段,如果你愿意的话可以在你的填充方法中初始化。

+0

我该如何解决这个问题? –

+0

@MatthewRomano查看你写的“public class Game {”,在“int [] space;”下插入一行。这宣告'空间'是你的游戏类的一个领域。然后在fill方法中,你可以从“space”之前删除“int []”,否则你会声明两次空间。 但是请注意,在调用填充之前,空间将保持未初始化状态。 –

+0

我做了这些改变,现在错误读取“不兼容的类型:void不能覆盖到int”它仍然突出显示“System.out.println(space [index ++]);}” –