如何用随机单个数字整数随机填充二维数组
how to randomly populate a 2D array with random single digit ints
我想创建一个地图 (AWorld),它基本上是一个 25x25 的单元格网格,边界为 |除了 0-9 之间随机出现的随机数之外,内部空白,根据它们的值 (eg.g 8),当虫子移动到它上面时,将提供一个 ABug 能量类型的对象。
例如
the world minus the horizontal lines.
现在我的数组似乎有一些 outofBoundsexception,我终其一生都无法弄清楚我做错了什么(我对 java 和一般的面向对象非常陌生,只是勉强得到了上学期的 c++ 挂起)。
我的代码如下
ABug class:
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;
}
}
世界Class:
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 天,似乎无法理解它。
在 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);
我想创建一个地图 (AWorld),它基本上是一个 25x25 的单元格网格,边界为 |除了 0-9 之间随机出现的随机数之外,内部空白,根据它们的值 (eg.g 8),当虫子移动到它上面时,将提供一个 ABug 能量类型的对象。
例如 the world minus the horizontal lines.
现在我的数组似乎有一些 outofBoundsexception,我终其一生都无法弄清楚我做错了什么(我对 java 和一般的面向对象非常陌生,只是勉强得到了上学期的 c++ 挂起)。
我的代码如下
ABug class:
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;
}
}
世界Class:
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 天,似乎无法理解它。
在 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);