Java OOP中的修复方法 [英] Fixing methods in Java OOP

查看:123
本文介绍了Java OOP中的修复方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到我的问题的问题。当我测试我的程序时,一切都会检查出来,除了一件事情。在执行程序时,在我的打印语句中,他们不会正常地更新健康状况。

I am having trouble finding the issue with my problem. Everything checks out right when I test my program except for one thing. In my print statements while I execute the program, they do not update the health correctly like they should.

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


推荐答案


在执行程序时,在我的打印语句中,他们不会正常地更新健康状况。

In my print statements while I execute the program, they do not update the health correctly like they should.

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

Your health field is a static field and thus a field of the class not of the instance. Every Dragon will share the exact same health value, which is not what you want. I suggest:


  • 首先制作更多的课程,因为您不应该在Dragon中拥有所有这些代码。你应该创建一个战士类,一个代表战斗龙的人,一个应该有自己的健康和类似的领域。

  • 这里的所有领域和方法大多数应该是非-静态的。唯一的例外,我可以看到应该是主要的方法,就是这样。请阅读为什么静态变量被认为是邪恶的?

  • 我将从Warrior和Dragon类中获取所有的用户界面代码,而不是将其重点放在维护自己的状态(字段值)和行为(方法)上。

  • 使用其他类进行用户交互。如果简单,这可能都是主要的方法,但是这种类型的代码并没有缩放。

  • 还有一个nitpick,但从不使用 == true == false ,而是使用更简洁易读的代码来测试布尔变量或表达式本身。所以代替 if(foo == true) if(bar == false) do if(foo) if(!bar)

  • First make more classes as you shouldn't have all this code within Dragon. You should create a Warrior class as well, one that represents the person fighting the Dragon, one that should have his own health and similar fields.
  • Most all the fields and methods here should be non-static. The only exception that I can see should be the main method, and that's it. Please read Why are static variables considered evil?
  • I would get all user interface code out of my Warrior and Dragon classes, and instead have them focus only on maintaining their own state (field values) and behaviors (methods).
  • Use another class for user interaction. If simple, this could all be in the main method, but this type of code does not "scale" well.
  • Also a nitpick, but never use == true or == false, but instead use more succinct and easily readable code that just tests the boolean variable or expression itself. So instead of if (foo == true) and if (bar == false) do if (foo) and 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
}

这篇关于Java OOP中的修复方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆