简体   繁体   中英

Assign values from a for loop into an array list of integers

For this stage of my code I am attempting to print a 8x8 grid of numbers randomly ranging from 1-8. I am struggling to assign the values in the fill arraylist to the random numbers in the two for loops. How do I solve this.

Here is my code:

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++]);
        }
   }
  }

It looks like your int[] space array is declared inside your fill method, and as such is not in scope outside of that method.

If you would like it to be in scope, consider declaring int[] space as a field of your class, which if you like can be initialized in your fill method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM