2015-11-04 2 views
0

Ниже приведена моя искусственная ошибка программы. Как бы я быть в состоянии получить доступ к coorordinates от основной, который установил пользователь, и назначить их в массив, который установлен в классе MyClass, заранее спасибо :)Доступ к переменным класса Java?

//imports 
import java.util.Scanner; 
import java.util.Random; 


public class main { 

    public static void main(String[] args) { 

     Scanner reader = new Scanner(System.in); 
     ABug[] BugObj = new ABug[4]; //Creating object BugObj of class ABug 
     int loop = 4; 
     int i = 0; 
     int cycles; 
     MyClass worldnew = new MyClass(); 



     System.out.println("Please enter the number of cycles you wish to run:"); 
     cycles = reader.nextInt(); //getting the amount of cycles to be run 
     System.out.print("____Current World____\n\n"); 
     worldnew.printWorld(); //calling method to print out world 

     System.out.println("____Key____\n_F_ - Food\n_O_ - Object\n_ _ - Space\nSymbol - Bug"); 


     do{ 

      BugObj[i] = new ABug(); //creating instance 


      System.out.print("Please enter the symbol which you wish to represent the bug:"); 
      BugObj[i].symbol = reader.next(); 
      System.out.print("Please enter the name of the bug:"); 
      BugObj[i].name = reader.next(); 
      System.out.println("Please enter the species of the bug:"); 
      BugObj[i].species = reader.next(); 
      System.out.println("Please enter the horizontal position of the bug:"); 
      BugObj[i].horpos = reader.nextInt(); 
      System.out.println("Please enter the vertical postion of the bug:"); 
      BugObj[i].vertpos = reader.nextInt(); 


      System.out.println("_______________ Bug " +(+i+1) + " _______________\n"); 
      System.out.println("Symbol: " + BugObj[i].symbol);  //Printing bug information out 
      System.out.println("Name: " + BugObj[i].name);   
      System.out.println("Species: " + BugObj[i].species); 
      System.out.println("Horizontal Position: " + BugObj[i].horpos); 
      System.out.println("Vertical Postion: " + BugObj[i].vertpos + "\n\n"); 
      move(BugObj[i]); 


      i++; 
      System.out.println("Would you like to enter another bug? \n 0-No, 1-Yes\n"); 
      loop = reader.nextInt(); 
     }while(loop == 1); 
    } 


    public static void move(ABug bug){ 
     Scanner reader = new Scanner(System.in); 
     System.out.println("Would you like this bug to move?\n 0-No, 1-Yes\n"); 
     if (reader.nextInt() == 0) 
     { 
      System.exit(0); 
     } 



     //get corordinate of bug 
     //set map[coor] = symbol 
     //print out map 


     int r = (int) (Math.random() * (2- -2)) + -2; 
     int originalHorpos = bug.horpos; 
     int originalVertpos = bug.vertpos; 
     bug.horpos = originalHorpos + r; 
     bug.vertpos = originalVertpos + r; 


     System.out.println("New Horizontal Position: " +bug.horpos); 
     System.out.println("New Vertical Postion: " +bug.vertpos); 

    } 

} 
enum Item { 
    OBJECT ('O'),FOOD ('F'), SPACE (' '); 

    private final char symbol; 
    Item(char symbol) { 
     this.symbol = symbol; 
    } 
    char getSymbol() { return symbol; } 
} 
class MyClass { 
    Item[][] map = new Item[15][25]; 
    public void printWorld() { 

     int v, h; //v - vert, h - hor 

     for (v=1; v<=15; ++v) 
     { 
      for (h=1; h<=25; ++h) 
      { 

       final Item[] items = {Item.OBJECT, Item.FOOD, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE}; 
       Random random = new Random(); 
       int selection = random.nextInt(items.length); 
       map[v-1][h-1] = items[selection]; 
       System.out.print(map[v-1][h-1].getSymbol() + "_"); 


      } 
      System.out.printf("\n"); 
     } 
    } 
} 
class ABug {     //ABug class 
    int horpos, vertpos, energy, id; 
    String species, name, symbol; 

} 

редактировать

package buglife; 
 

 

 

 
//imports 
 
import java.util.Scanner; 
 
import java.util.Random; 
 

 

 
public class main { 
 

 
\t public static void main(String[] args) { 
 

 
\t \t Scanner reader = new Scanner(System.in); 
 
\t \t ABug[] BugObj = new ABug[4]; //Creating object BugObj of class ABug 
 
\t \t int loop = 4; 
 
\t \t int i = 0; 
 
\t \t int cycles; 
 
\t \t MyClass worldnew = new MyClass(); 
 

 

 

 
\t \t System.out.println("Please enter the number of cycles you wish to run:"); 
 
\t \t cycles = reader.nextInt(); //getting the amount of cycles to be run 
 
\t \t System.out.print("____Current World____\n\n"); 
 
\t \t worldnew.printWorld(); //calling method to print out world 
 

 
\t \t System.out.println("____Key____\n_F_ - Food\n_O_ - Object\n_ _ - Space\nSymbol - Bug"); 
 

 

 
\t \t do{ 
 

 
\t \t \t BugObj[i] = new ABug(); //creating instance 
 

 

 
\t \t \t System.out.print("Please enter the symbol which you wish to represent the bug:"); 
 
\t \t \t BugObj[i].symbol = reader.next(); 
 
\t \t \t System.out.print("Please enter the name of the bug:"); 
 
\t \t \t BugObj[i].name = reader.next(); 
 
\t \t \t System.out.println("Please enter the species of the bug:"); 
 
\t \t \t BugObj[i].species = reader.next(); 
 
\t \t \t System.out.println("Please enter the horizontal position of the bug:"); 
 
\t \t \t BugObj[i].horpos = reader.nextInt(); 
 
\t \t \t System.out.println("Please enter the vertical postion of the bug:"); 
 
\t \t \t BugObj[i].vertpos = reader.nextInt(); \t 
 

 

 
\t \t \t System.out.println("_______________ Bug " +(+i+1) + " _______________\n"); 
 
\t \t \t System.out.println("Symbol: " + BugObj[i].symbol);  //Printing bug information out 
 
\t \t \t System.out.println("Name: " + BugObj[i].name);   
 
\t \t \t System.out.println("Species: " + BugObj[i].species); 
 
\t \t \t System.out.println("Horizontal Position: " + BugObj[i].horpos); 
 
\t \t \t System.out.println("Vertical Postion: " + BugObj[i].vertpos + "\n\n"); 
 
\t \t \t move(BugObj[i], worldnew); 
 

 

 
\t \t \t i++; 
 
\t \t \t System.out.println("Would you like to enter another bug? \n 0-No, 1-Yes\n"); 
 
\t \t \t loop = reader.nextInt(); 
 
\t \t }while(loop == 1); 
 
\t } 
 

 

 
\t public static void move(ABug bug, MyClass wolrdnew){ 
 
\t \t Scanner reader = new Scanner(System.in); 
 
\t \t System.out.println("Would you like this bug to move?\n 0-No, 1-Yes\n"); 
 
\t \t if (reader.nextInt() == 0) 
 
\t \t { 
 
\t \t \t System.exit(0); 
 
\t \t } 
 

 
\t \t int x = bug.horpos; 
 
\t \t int y = bug.vertpos; 
 

 

 
\t \t worldnew.setMap(x,y,bug.symbol()); 
 
\t \t worldnew.printWorld(); 
 

 
\t \t //get corordinate of bug 
 
\t \t //set map[coor] = symbol 
 
\t \t //print out map 
 

 

 
\t \t int r = (int) (Math.random() * (2- -2)) + -2; 
 
\t \t int originalHorpos = bug.horpos; 
 
\t \t int originalVertpos = bug.vertpos; 
 
\t \t bug.horpos = originalHorpos + r; 
 
\t \t bug.vertpos = originalVertpos + r; 
 

 

 
\t \t //bug.horpos += r; 
 
\t \t //bug.vertpos += r; 
 

 
\t \t System.out.println("New Horizontal Position: " +bug.horpos); 
 
\t \t System.out.println("New Vertical Postion: " +bug.vertpos); 
 

 
\t } 
 
} 
 

 
//public void smellFood (Direction d){ 
 
// \t int MaxSensingDist = 2; 
 
//} 
 

 
//public void getRandomDirectionToMove (Direction d){ 
 
// 
 
//} 
 

 
//public void getDirectionOfFood(){ 
 

 
//} 
 

 

 
enum Item { 
 
\t OBJECT ('O'),FOOD ('F'), SPACE (' '); 
 

 
\t private final char symbol; 
 
\t Item(char symbol) { 
 
\t \t this.symbol = symbol; 
 
\t } 
 
\t char getSymbol() { return symbol; } 
 
} 
 
class MyClass { 
 

 
\t public void setMap(int x, int y, Item symbol) 
 
\t { 
 
\t \t this.map[x][y] = symbol; 
 
\t } 
 
\t Item[][] map = new Item[15][25]; 
 
\t public void printWorld() { 
 

 
\t \t int v, h; //v - vert, h - hor 
 

 
\t \t for (v=1; v<=15; ++v) 
 
\t \t { 
 
\t \t \t for (h=1; h<=25; ++h) 
 
\t \t \t { 
 

 
\t \t \t \t //map[0][0] = Item.TREE; 
 
\t \t \t \t //map[0][1] = Item.FOOD; 
 
\t \t \t \t //System.out.print(map[0][0].getSymbol()); 
 
\t \t \t \t //System.out.print(map[0][1].getSymbol()); 
 

 
\t \t \t \t //final String[] items = {"F", "O", ". ", ". ", ". ", ". ", ". ", ". "}; 
 
\t \t \t \t //Random random = new Random(); 
 
\t \t \t \t //int index = random.nextInt(items.length); 
 
\t \t \t \t //System.out.printf(items[index] + "\t"); 
 

 
\t \t \t \t final Item[] items = {Item.OBJECT, Item.FOOD, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE}; 
 
\t \t \t \t Random random = new Random(); 
 
\t \t \t \t int selection = random.nextInt(items.length); 
 
\t \t \t \t map[v-1][h-1] = items[selection]; 
 
\t \t \t \t System.out.print(map[v-1][h-1].getSymbol() + "_"); 
 

 

 
\t \t \t } 
 
\t \t \t System.out.printf("\n"); 
 
\t \t } 
 
\t } 
 
} 
 
class ABug {     //ABug class 
 
\t int horpos, vertpos, energy, id; 
 
\t String species, name, symbol; 
 

 
}

+2

Check [Как добытчиками и сеттеров работы ?] (http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – sam

ответ

1

как насчет просто

int x = bug.get(horpos); 
int y = bug.get(vertpos); 

worldnew.setMap(x,y,bug.getSymbol()); 

worldnew.printWorld(); 

и в MyClass, добавить новый метод, как

void setMap(int x, int y, String symbol) 
{ 
    this.map[x][y] = symbol; 
} 

вам, возможно, потребуется внести некоторые изменения в соответствии с вашим кодом, но основной идея остается той же

Редактировать

вам также нужно изменить

move(BugObj[i]); 

в

move(BugObj[i],worldnew); 

и изменить определение вашей функции перемещения в

public static void move(ABug bug, MyClass worldnew) 
+1

Спасибо, у меня есть добавили его в мой код и изменили его, однако, когда я вставляю 'worldnew.setMap (x, y, bug.symbol()); worldnew.printWorld(); ' в метод перемещения, его высказывание, что «worldnew не разрешено. – Logic

+0

thats, потому что worldnew не существует в этой функции. см. редактирование на мой ответ – AbtPst

+0

Спасибо, я добавил, что в, однако его еще не признает worldnew.setMap. Есть ли способ разрешить доступ? – Logic

Смежные вопросы