对于我的代码的这个阶段,我试图从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++]);
}
}
}
你已经在'fill'中声明了'fill'数组作为局部变量 - 所以它的作用域限于那个方法。也许你打算把它宣布为一个领域? (顺便说一下,我不认为声明64个单独的变量是一个好主意......我建议*只是*有一个矩阵......) –
此代码不能编译。你在使用IDE吗? –
是的,代码不会编译循环的第二个结尾。 –