为什么我无法访问worldnew方法java?

时间:2015-11-15 14:43:39

标签: java variables methods

我遇到了麻烦,因为我的代码无法识别worldnew,任何想法?问题在于移动方法,我在评论我想要实现的目标。我不明白的是bug.hospos如何获取访问但是worldnew.setMap不是?

package buglife;

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], worldnew);

            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, MyClass wolrdnew){
        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);
        }

        int x = bug.horpos;
        int y = bug.vertpos;
        String a = bug.symbol;

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

        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 {
    public void setMap(int x, int y, Item symbol)
    {
        this.map[x][y] = symbol;
    }
    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;
    String symbol;

}

2 个答案:

答案 0 :(得分:1)

您在方法参数名称中存在拼写错误:

public static void move(ABug bug, MyClass wolrdnew){

它是wolrdnew而不是worldnew。这就是为什么你无法访问这样的变量。

答案 1 :(得分:0)

移动中的变量名称有拼写错误 - wolrdnew

相关问题