setLocation(x,y)上的Java错误

时间:2016-05-21 07:10:41

标签: java

我正在编写一个简单的鼠标和猫游戏。 但是当我尝试使用setLocation时,我总是遇到调试错误。

你能告诉我怎么解决它?我不认为有什么不妥。

import java.awt.Point;
import java.util.Random;

public abstract class Animal {

Point location_;
String name_;
Random rng_;

public Animal() {
}

 public Animal( String name, Random rng ){
    name_=name;
    rng_=rng;
}


abstract void move();

 public String getName(){
     return name_;
 }

 public Point getLocation(){
     return location_;
 }

 public void setStartLocation(){
     int x = rng_.nextInt(4);
     int y = rng_.nextInt(4);


// error 
    location_.setLocation(x, y);
 }

}

另一个档案:

import java.util.Random;

public class Cat extends Animal {



public Cat(String name, Random rng){
    super(name,rng);    
}


void move() {
    int i = rng_.nextInt(3);
    int x = (int) location_.getX();
    int y = (int) location_.getY();

    if(i==0 && y<4){

        int newY = y++;

        location_.move(x,newY);
    }

    else if (i==1 && y>0){

        int newY = y--;

        location_.move(x,newY);
    }

    else if (i==2 && x>0){

        int newX = x--;

        location_.move(newX,y);
    }

    else if (i==3 && x<4){

        int newX = x++;

        location_.move(newX,y);
    }

    else move();

}



}

另一个档案:

import java.util.Random;

public class Mouse extends Animal {

String mobility;

public Mouse(String name, Random rng){
    super(name,rng);    
}


void move() {
    int i = rng_.nextInt(3);
    int x = (int) location_.getX();
    int y = (int) location_.getY();

    if(i==0 ){

        int newY = y++;

        location_.move(x,newY);
    }

    else if (i==1){

        int newY = y--;

        location_.move(x,newY);
    }

    else if (i==2){

        int newX = x--;

        location_.move(newX,y);
    }

    else if (i==3){

        int newX = x++;

        location_.move(newX,y);
    }



}

public String checkMoblity(){
    int x = (int) location_.getX();
    int y = (int) location_.getY();

    if(x==-1 || x==5){
        if(y==0 || y==2 || y==4){
            return "drowned";
        }

        else return "escaped";
    }

    else if(y==-1 || y==5){
        if(x==0 || x==2 || x==4){
            return "drowned";
        }

        else return "escaped";
    }

    else  return null;

}

}

另一个档案:

 import java.util.Random;

public class Chase {

public  void playGame(){

    Random rand = new Random();

//error
    Cat cat = new Cat("Tom", rand);
    Mouse mouse = new Mouse("Jerry", rand);

    do{
        cat.setStartLocation();
        mouse.setStartLocation();
    }while(cat.getLocation()== mouse.getLocation());

    String status = Status(cat,mouse);
    boolean end = end(cat, mouse);

    do{
        mouse.move();

        if(end==false){
        cat.move();
        }

    }while(end==false);

    if (status == "drowned"){
        System.out.println(mouse.getName() + " drowned!");
    }

    else if (status == "escaped"){
        System.out.println(mouse.getName() + " escaped!");
    }

    else if (status == "snack"){
        System.out.println(cat.getName() + " got the snack!");
    }


}



public  String Status(Cat cat, Mouse mouse){
    if(mouse.checkMoblity()== "drowned"){
        return "drowned";
    }

    else if(mouse.checkMoblity() == "escaped"){
        return "escaped";
    }

    else if(mouse.getLocation()==cat.getLocation()){
        return "snack";
    }

    else return null;
}

public  boolean end(Cat cat, Mouse mouse){
    if(mouse.checkMoblity()== "drowned"){
        return true;
    }

    else if(mouse.checkMoblity() == "escaped"){
        return true;
    }

    else if(mouse.getLocation()==cat.getLocation()){
        return true;
    }

    else return false;
}



public static void main(String[] args){
    Chase chase = new Chase();

//error
    chase.playGame();
}
}

1 个答案:

答案 0 :(得分:1)

在使用之前你应该instantiate an object

public void setStartLocation(){
    int x = rng_.nextInt(4);
    int y = rng_.nextInt(4);

    location_ = new Point(x, y);
}

如果未实例化对象setLocation(int, int),则无法使用setter方法Point location_