samedi 1 août 2015

Random Char in 2D arrays

Hi i am new at java coding and am trying to create random numbers(which i have done) and i am trying to assign this random numbers as coordinates into the 2D array and print 'A' at the coordinates. Any help is appreciated.

package training;

import java.util.Random;

public class Training {

    public static void main(String[] args) {

        char[][] values = new char[10][10];
        int foodX[] = new int[15];
        for (int i = 1; i < foodX.length + 1; i++) {
            int minFood = 0;
            int maxFood = 10;
            int randNum1 = minFood + (int) (Math.random() * (maxFood - minFood) + 1);
            int minFoodY = 0;
            int maxFoodY = 10;
            int randNum2 = minFoodY + (int) (Math.random() * (maxFoodY - minFoodY) + 1);
            for (int j = 1; j < foodX.length + 1; j++) {
                values[randNum1][randNum2] = 'A';

            }

        }

    // Assign three elements within it.
        // Loop over top-level arrays.
        for (int i = 0; i < values.length; i++) {

            // Loop and display sub-arrays.
            char[] sub = values[i];

            for (int x = 0; x < sub.length; x++) {
                System.out.print(sub[x] + " ");
            }
            System.out.println();

        }

    }
}




Aucun commentaire:

Enregistrer un commentaire