[英]how to randomly populate a 2D array with random single digit ints
我想創建一個地圖(AWorld),該地圖基本上是一個25x25的以|為邊界的單元格網格 和內部空白,除了0-9之間的隨機數的隨機出現以外,根據它們的值(例如8),當錯誤在其上移動時,將提供ABug能量類型的對象。
現在我的數組似乎有一些outofBoundsexception,並且我一生無法弄清楚我做錯了什么(我對Java和Objectl面向對象非常陌生,上個學期才勉強掌握了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;
}
}
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("|");
}
}
}
主要:
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() );
////////////////////////////////////////////////////////
}
}
我已經堅持了三天,似乎無法解決這個問題。
在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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.