在Java OOP中一次使用2个对象

时间:2015-09-05 05:21:09

标签: java oop object

我正在尝试创建两个互相争斗的对象。两个对象(战士和龙)都是子类。我的目标是能够让一个对象攻击另一个对象,并显示结果。使用我当前的代码,我不断抛出异常。

import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author Brenton
 */
public class Fighter {

    private String name;
    private int attack;
    private int level = 1;
    private int health = 50;
    private boolean isAlive = true;

    private Fighter fighterOne;
    private Fighter fighterTwo;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAttack() {
        Random generator = new Random();
        attack = generator.nextInt(10) * level + 1;
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getLevel() {
        return this.level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getHealth() {
        if(this.health <= 0)
        {
            this.health = 0;
        }
        return this.health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public boolean isAlive() {
        if(this.health <= 0)
        {
            this.isAlive = false;
        }
        return this.isAlive;
    }

    public static String getWelcome() {
        String welcome = "Hello and welcome to FightClub, do you wish to fight, yes or no? ";
        return welcome;
    }

    public String getPunch(Fighter fighterTwo) {
        this.fighterTwo = fighterTwo;
        String hit = "You choose to punch the other fighter and dealt" + getAttack() + " your opponent now has " + fighterOne.decreaseHitPoints(fighterTwo) + " remaining";
        return hit;
    }

    public int decreaseHitPoints(Fighter fighterTwo) {
        this.fighterTwo = fighterTwo;
        int health = fighterTwo.getHealth();
        int attack = getAttack();
        health = health - attack;
        return health;
    }

    public static String invalidInput() {
        String invalid = "I am sorry that is not a valid input option ";
        return invalid;
    }

    public void getWinner(Fighter fighterOne, Fighter fighterTwo) {
        this.fighterOne = fighterOne;
        this.fighterTwo = fighterTwo;
        if(fighterOne.isAlive() == false && fighterTwo.isAlive() == false)
        {
            System.out.println("Both fighters have fallen heroically");
        }
        else if(fighterOne.isAlive() == true && fighterTwo.isAlive() == false)
        {
            System.out.println(fighterOne + " is victorious! ");
        }
        else if(fighterOne.isAlive() == false && fighterTwo.isAlive() == true)
        {
            System.out.println(fighterTwo + " is victorious! ");
        }
        else
        {
            System.out.println("ERROR ERROR ERROR");
        }      
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        Fighter a = new Warrior();
        Fighter b = new Dragon();

        System.out.print(getWelcome());     
        while(in.hasNextLine()) 
        {
            switch(in.nextLine()) 
            {
                case "no":
                    System.out.println("Wow, you are not even gonna try, you have lost!");
                    break;
                case "yes":
                    System.out.println("Let the fight begin! ");
                    while(a.isAlive() && b.isAlive()) 
                    {
                        System.out.println("Do you want to punch, kick, or headbutt the other fighter? ");
                        switch(in.nextLine()) 
                        {
                            case "punch":
                                System.out.println(a.getPunch(b));
                                break;
                            /*case "kick":
                                System.out.println(a.getKick(b));
                                break;
                            case "headbutt":
                                System.out.println(a.getHeadbutt(b));
                                break;*/
                            default :
                                System.out.println(invalidInput());
                                break;
                        }
                    }
                default:
                    System.out.println(invalidInput());
                    break;  
            }//end of first switch statement
        }//end of first while loop
    }//end of main   
}

这是错误

Exception in thread "main" java.lang.NullPointerException
    at Dragonslayer.Fighter.getPunch(Fighter.java:79)
    at Dragonslayer.Fighter.main(Fighter.java:140)
Java Result: 1

1 个答案:

答案 0 :(得分:1)

使用getPunch(...)方法

替换

fighterOne.decreaseHitPoints(fighterTwo) // here fighterOne can be null

this.decreaseHitPoints(fighterTwo)  // use this which is nothing but your invoking object a.
相关问题