如何随机填充随机单位数的二维数组

时间:2015-11-12 01:13:58

标签: java arrays random indexoutofboundsexception populate

我想创建一个地图(AWorld),它基本上是一个25x25的单元格网格并且除了0-9之间随机数的随机出现之外的空白,根据它们的值(例如g 8)将在bug移动时提供ABug能量类型的对象。

e.g。 the world minus the horizontal lines.

现在我似乎对我的数组有一些outofBoundsexception,我不知道我的生活中弄清楚我做错了什么(我非常新的java和对象取向一般只有熊在上个学期得到了c ++) 。

我的代码如下

ABug班:

public class ABug 

{


public static void main(){

}

    private String species = new String(); //instance variables (defines data of object)
    private String name = new String();
    private String description = new String();
    private char symbol;
    private int hPossistion, vPossistion, energy, iD;

    public ABug(){

    }

    public ABug (String s, String n, String d, int h, int v, int e, int i){
        this.species = s;
        this.name = n;
        this.description = d;
        this.symbol = s.charAt(0);
        this.hPossistion = h;
        this.vPossistion = v;
        this.energy = e;
        this.iD = i;
    }




    //setters
    public void setSpecies(String s){
        this.species = s;
    }

    public void setName(String n){
        this.name = n;
    }

    public void setSymbol(char symbol){
        this.symbol = symbol;
    }

    public void setHPossistion(int x){
        this.hPossistion = x;
    }

    public void setVPossistion(int y){
        this.vPossistion = y;
    }

    public void setEnergy(int energy){
        this.energy = energy;
    }

    public void setID(int i){
        this.iD = i;
    }

    public void setDescription(String d){
        this.description = d;
    }

    //getters
    public String getSpecies(){
        return this.species;
    }

    public String getName(){
        return this.name;
    }

    public char getSymbol(){
        return this.symbol;
    }

    public int getHPossistion(){
        return this.hPossistion;
    }

    public int getVPossistion(){
        return this.vPossistion;
    }

    public int  getEnergy(){
        return this.energy;
    }

    public int getID(){
        return this.iD;
    }

    public String getDescription(){
        return this.description;
    }

    public String toString(){
        String BugData;
        BugData = name + " " + symbol + "\n" + species + "\n" + description;
        return BugData;
    }






}

世界级:

package finalversion;

import java.util.Arrays;
import java.util.Random;

public class AWorld {

public static int RandFood(int min, int max){
    Random rand = new Random(); // Initializes the random function. 

    int RandNum = rand.nextInt((max - min) +1) + min; //generates a random number within the max and min ranges 
    return RandNum; //returns the random number 
}

    public static void main(String[] args){

    ABug bug1 = new ABug();     
    int row = 25;
    int column = 25;
    char [ ][ ] map = new char[row][column];
    Random rand = new Random();
    int hPossistion = rand.nextInt();
    int vPossistion = rand.nextInt();

    for (int i=0; i<column; i++)
    {
    map[hPossistion][vPossistion] = (char) RandFood(0,9); > // this is where is is getting the error.

    }
    for (int i=0; i<row; i++)
    {

        for (int x1=0; x1<column; x1++)
        {
            map[i][x1] = ' ';


        }

    }
        map[bug1.getHPossistion()][bug1.getVPossistion()] = bug1.getSymbol(); // gets the bugs x and y possistion in the array (user defined) and puts it into the symbol variable 


    for (int i=0; i<row; i++) //these next two for loops print the array to the screen 
    {
        System.out.print("|");
        for (int x1=0; x1<column; x1++) 
        {
            map[i][x1] = ' ';
            System.out.print(map[i][x1]);

        }
        System.out.println("|");
    }






}  

}

主:

import java.util.Scanner;

public class UserInput {

public static void main(String[] args)
{
    Scanner Scan = new Scanner(System.in);
    AWorld World = new AWorld();
    ABug Abugs[];
    int top=0;
    System.out.println("please enter how many bugs you want to create?");
    int  num = new Integer( Scan.nextLine() );
    Abugs = new ABug[num];

    int x=0;

    ABug bug = new ABug(" ", " ", " ", 0, 0, 0, 0);

    ///////////////////////////////////////////////////////////////

    System.out.println("please enter a Species Name");
    String s = Scan.nextLine();
    bug.setSpecies(s);


    /////////////////////////////////////////////// 

    System.out.println("please enter a name for the bug");
    String n = Scan.nextLine();
    bug.setName(n);

    //////////////////////////////////////////////


    System.out.println("please enter a Symbol for the bug");
    char sy = Scan.nextLine().charAt(0);
    bug.setSymbol(sy);

    ///////////////////////////////////////////////////////////

    System.out.println("please enter a description for the bug");
    String d = Scan.nextLine();
    bug.setDescription(d);

    /////////////////////////////////////////////////////

    System.out.println("please enter the energy for the bug");
    int e = new Integer( Scan.nextLine() );
    bug.setEnergy(e);

    ///////////////////////////////////////////////////////

    System.out.println("please enter the horisontal position for the bug");
    int h = new Integer( Scan.nextLine() );
    bug.setHPossistion(h); 

    /////////////////////////////////////////////////////////////

    System.out.println("please enter the vertical position for the bug");
    int v = new Integer( Scan.nextLine() );
    bug.setVPossistion(v);

    ////////////////////////////////////////////////////////////////

    Abugs[x] = bug;

    System.out.println( Abugs[x].toString() );


    ////////////////////////////////////////////////////////

}

}

我被困在这3天了,似乎无法绕过它。

1 个答案:

答案 0 :(得分:2)

在AWorld中,您将在数组中生成随机索引。但这些都需要限制:

    int hPossistion = rand.nextInt();
    int vPossistion = rand.nextInt();

    for (int i=0; i<column; i++)
    {
    map[hPossistion][vPossistion] = (char) RandFood(0,9); > // this is where is is getting the error.

应该是

int hPossistion = rand.nextInt(column);
int vPossistion = rand.nextInt(row);