对于我的代码的这个阶段,我试图随机打印一个8x8的数字网格,范围从1-8。我正在努力将填充arraylist中的值分配给两个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++]);
}
}
}
答案 0 :(得分:0)
看起来你的int []空间数组是在fill方法中声明的,因此不在该方法之外的范围内。
如果您希望它在范围内,请考虑将int []空间声明为您的类的字段,如果您愿意,可以在填充方法中初始化。