Java OOP中的修复方法

时间:2015-09-01 23:50:25

标签: java oop debugging methods

我无法找到问题的问题。除了一件事,当我测试我的程序时,一切都会正确检查。在我执行程序时的打印语句中,它们不会像应该的那样正确更新运行状况。

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

public class Dragon {

    private static int health;
    private static int attack;
    private static boolean isAlive = true;

    private static int dragonHealth;
    private static int dragonAttack;
    private static boolean isDragonAlive = true;

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

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

    public int getAttack() {
        return attack;
    }

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

    public static int getDamage()
    {
        Random generator = new Random();
        int attack = generator.nextInt(10) + 1;
        health = health - dragonAttack;
        return attack;
    }

    public static boolean getAlive()
    {
        if(getHealth() <= 0)
        {
            isAlive = false;
        }
        return isAlive;
    }    

    ///////////////////////////////////////////////////////

    public static int getDragonHealth()
    {
        if(dragonHealth <= 0)
        {
            dragonHealth = 0;
        }
        return dragonHealth;
    }

    public void setDragonHealth(int dragonHealth)
    {
        this.dragonHealth = dragonHealth;
    }

    public int getDragonAttack() {
        return dragonAttack;
    }

    public void setDragonAttack(int dragonAttack) {
        this.dragonAttack = dragonAttack;
    }

    public static int getDragonDamage()
    {
        Random generator = new Random();
        int dragonAttack = generator.nextInt(10) + 1;
        dragonHealth = dragonHealth - attack;
        return dragonAttack;
    } 

    public static boolean getDragonAlive()
    {
        if(getDragonHealth() <= 0)
        {
            isDragonAlive = false;
        }
        return isDragonAlive;
    }    

    /////////////////////////////

    public String getWelcome()
    {
        String welcome = "Hello and welcome to Dragonslayer!";
        return welcome;
    }

    public static String getStab()
    {
        String stab = "You choose to stab the dragon and dealt " + getDamage() + " damage. The dragon now has " + getDragonHealth() + " health remaining.";
        return stab;
    }

    public static String getSlash()
    {
        String slash = "You choose to slash the dragon and dealt " + getDamage() + " damage. The dragon now has " + getDragonHealth() + " health remaining.";
        return slash;
    }

    public static String getCut()
    {
        String cut = "You choose to cut the dragon and dealt " + getDamage() + " damage. The dragon now has " + getDragonHealth() + " health remaining.";
        return cut;
    }

    public static String dragonAttack()
    {
        String dragonsAttack = "The dragon has done " + getDragonDamage() + " to you. You now have " + getHealth() + " health remaining.";
        return dragonsAttack;
    }

    public static String getWinner()
    {
        String result = "";
        if(getAlive() == false && getDragonAlive() == false)
        {
            result = "It is a horrid day today, as both you and the dragon have fallen.";
        }
        else if(getAlive() == true && getDragonAlive() == false)
        {
            result = "Congratulations you have killed the dragon, and survived!";
        }
        else if(getAlive() == false && getDragonAlive() == true)
        {
            result = "You have sadly fallen to the dragon, better luck next time.";
        }
        else
        {
            result = "SOMETHING WENT WRONG!!!";
        }
        return result;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("You come across the dragon and you have two options.  Do you run or fight? ");


        Dragon dragon1 = new Dragon();
        dragon1.setHealth(50);

        Dragon dragon2 = new Dragon();
        dragon2.setDragonHealth(50);

        while(in.hasNextLine())
        {
            switch(in.nextLine())
            {
                case "run":
                    System.out.println("I am so sorry, you could not outrun the dragon, you have been killed!");
                    break;
                case "fight":
                    while(getAlive() && getDragonAlive())
                    {
                        System.out.println("Do you want to stab, slash, or cut the dragon? ");
                        switch(in.nextLine())
                        {
                            case "stab":
                                System.out.println(getStab() + "\n" + dragonAttack());
                                break;
                            case "slash":
                                System.out.println(getSlash() + "\n" + dragonAttack());
                                break;
                            case "cut":
                                System.out.println(getCut() + "\n" + dragonAttack());
                                break;
                            default:
                                System.out.println("I am sorry that is not valid, try again. ");
                        }
                    }
                    break;
                default:
                    System.out.println(getWinner());
                    break;
            }

            System.out.println("Congratulations, you have slayed the dragon!");
            break;
        }//end of while loop in.hasNextLine().
    }//end of main
}//end of class

2 个答案:

答案 0 :(得分:4)

  

在我执行程序时的打印语句中,它们不会像应该的那样正确更新运行状况。

您的健康字段是静态字段,因此的字段不是实例的字段。每个龙将分享完全相同的健康值,这不是你想要的。我建议:

  • 首先制作更多课程,因为你不应该在Dragon中拥有所有这些代码。你也应该创建一个Warrior类,一个代表与龙战斗的人,一个应该有自己的健康和类似领域的人。
  • 这里的大多数字段和方法都应该是非静态的。我能看到的唯一例外应该是主要方法,就是这样。请阅读Why are static variables considered evil?
  • 我会从Warrior和Dragon类中获取所有用户界面代码,而是让他们只关注维护自己的状态(字段值)和行为(方法)。
  • 使用其他类进行用户交互。如果简单,这可能都在main方法中,但是这种类型的代码并没有&#34; scale&#34;好。
  • 也是一个挑剔,但从不使用== true== false,而是使用更简洁,易读的代码,只测试布尔变量或表达式本身。因此,而不是if (foo == true)if (bar == false)执行if (foo)if (!bar)

例如:

class Creature {
    private int health;
    private int attack;
    private String name;

    public String getName() {
        return name;
    }

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

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health < 0 ? 0 : health;
    }

    public int getAttack() {
        return attack;
    }

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

    public boolean isAlive() {
        return health > 0;
    }

}

class Dragon extends Creature {
    // add Dragon specific methods
}

class Warrior extends Creature {
    // add Warrier specific methods
}

答案 1 :(得分:1)

我想你要改变

public static int getDamage()
{
    Random generator = new Random();
    int attack = generator.nextInt(10) + 1;
    health = health - dragonAttack;
    return attack;
}

要:

 public static int getDamage()
{
    Random generator = new Random();
    setAttack(generator.nextInt(10) + 1);
    health = health - dragonAttack;
    return attack;
}

同样适用于getDragonDamage ...... 你正在设置一个局部变量(在这种情况下是攻击)并期望它在getDragonDamage中更新