Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用随机的一位数整数随机填充二维数组_Java_Arrays_Random_Indexoutofboundsexception_Populate - Fatal编程技术网

Java 如何使用随机的一位数整数随机填充二维数组

Java 如何使用随机的一位数整数随机填充二维数组,java,arrays,random,indexoutofboundsexception,populate,Java,Arrays,Random,Indexoutofboundsexception,Populate,我想创建一个映射(AWorld),它基本上是一个25x25的网格,网格内有|和空白,而不是随机出现的0-9之间的随机数,根据它们的值(例如G8),当bug移动到它上面时,它将提供一个ABug能量类型的对象 e、 g。 现在我似乎对数组有一些例外,我不能为我的生活找出我做错了什么(我对java和面向对象的新认识,只是在最后一次得到C++的挂起)。p> 我的代码如下 ABug类: public class ABug { public static void main(){ }

我想创建一个映射(AWorld),它基本上是一个25x25的网格,网格内有|和空白,而不是随机出现的0-9之间的随机数,根据它们的值(例如G8),当bug移动到它上面时,它将提供一个ABug能量类型的对象

e、 g。

现在我似乎对数组有一些例外,我不能为我的生活找出我做错了什么(我对java和面向对象的新认识,只是在最后一次得到C++的挂起)。p> 我的代码如下

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;
    }






}
AWorld类:

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("|");
    }






}  

}
}


这件事我已经坚持了三天了,似乎我的脑子都转不过来了

在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 hposistion=rand.nextInt(25)
(与
vposistion
相同)。返回“概率(近似)相等的所有2^32个可能的int值。”没有问题。如果需要,您可以通过单击左上角的复选标记将其标记为问题的答案来关闭问题
    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);