2

These are the classes and the main method I used. It is supposed to output prompts to the user in order to manage little "battles". Why does it run yet output nothing?

public class Hero 
{
    private String name;
    private int health;
    private int strength;
    private int agility;
    private int stamina;
    Die die1;
    Die die2;
    Die die3;

    public Hero(String name) 
    {
        this.name = name;
        this.health = 50;
        this.stamina = 25;
        this.strength = 5;
        this.agility = 5;
    }

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

    public String getName() 
    {
        return name;
    }

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

    public int getHealth() 
    {
        return health;
    }

    public void setStrength(int strength) 
    {
        this.strength = strength;
    }

    public int getStrength() 
    {
        return strength;
    }

    public void setAgility(int agility) 
    {
        this.agility = agility;
    }

    public int getAgility() 
    {
        return agility;
    }

    public void setStamina(int stamina) 
    {
        this.stamina = stamina;
    }

    public int getStamina() 
    {
        return stamina;
    }

    public int addHealth() 
    {
        if(health < 45 || health >= 0) 
        {
            health = health + 5;
        }
        return health;
    }

    public int removeHealth() 
    {
        if(health <= 50 || health >= 5) 
        {
            health = health - 5;
        }
        return health;
    }

    public int addStamina() 
    {
        if(stamina <= 20 || stamina >= 0) 
        {
            stamina = stamina + 5;
        }
    return stamina;
    }

    public int removeStamina() 
    {
        if(stamina <= 25 || stamina >= 5) 
        {
            stamina = stamina - 5;
        }
    return stamina;
    }

    private void rest() 
    {
        health = health + addHealth();
        stamina = stamina + addStamina();
    }

    public int attack(int damage) 
    {
        die1 = new Die(20);
        die1.roll();

        if(stamina <= 5) 
        {
            rest();
        }
        else 
        {
            damage = strength + die1.getValue();
            stamina = stamina - 6;
        }
        return damage;
    }

    public int defend(int block) 
    {
        die1 = new Die(20);
        die1.roll();

        if(stamina <= 3) 
        {
            rest();
        }

        if(die1.getValue() <= 6) 
        {
            block = getAgility() + (die1.getValue() * 2);
        }
        else 
        {
            block = getAgility() + (die1.getValue());
        }
        stamina = stamina - 4;
        return block;
    }

    public void printStats() 
    {
        System.out.println("Thy hero's name is: " + getName());
        System.out.println("Thy strengh is: " + getStrength());
        System.out.println("Thy agility is: " + getAgility());
        System.out.println("Thy stamina is " + getStamina());
        System.out.println("Thy health is: " + getHealth());
    }
}
public class Battle_Manager
{
    public static void main(String[] args) 
    {
        Hero hA = new Hero("Ayy");
        Hero hB = new Hero("Bee");

        Die die1 = new Die(12);
        Die die2 = new Die(8);
        Die die3 = new Die(6);

        die1.roll();
        die2.roll();
        die3.roll();
    }   
}

import java.util.Random;

public class Die 
{
    private int sides;
    private int dieValue;
    
     public void roll() 
    {
        Random r = new Random();
        dieValue = (r.nextInt(sides) + 1);
    }
     
    public int getSides() 
    {
        return sides;
    }
    
    public void setSides(int sides) 
    {
        this.sides = sides;
    }
    
    public Die() 
    {
        sides = 1;
        dieValue = 1;
    }
    
    public Die (int sides) 
    {
        sides = 1;
        dieValue = 1;
        
        if(sides >= 0) 
        {
            this.sides = sides;
        }
    }
    
    public int getValue() 
    {
        int value = dieValue;
        return value;
    }
}

These are the classes and the main method I used. It is supposed to output prompts to the user in order to manage little "battles". Why does it run yet output nothing?

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Manny A
  • 29
  • 2
  • 5
    JavaScript !== Java – epascarello Mar 29 '18 at 17:14
  • 1
    It looks like you are not ever calling `printStats` on your `Hero` objects. After rolling the dice try adding `hA.printStats()` and `hB.printStats()`. – burnttoast11 Mar 29 '18 at 17:17
  • 1
    *"Why does it run yet output nothing?"* Because you never `print` anything!!! – Andreas Mar 29 '18 at 17:22
  • Thank you, that did work! How would I improve this code to perform this action "Roll one 12 sided die, one 10 sided die, and one 8 sided die. Assign the highest value to strength. Set agility to the sum of the other two values."? – Manny A Mar 29 '18 at 17:22
  • @MannyAvila You already did the 3 rolls. Next step is for you to write code to find highest value, using `if` statements. Try that, and write a **new** question if that stumps you. – Andreas Mar 29 '18 at 17:25
  • Your constructor Die (int sides) doesn't make any sense. – isaace Mar 29 '18 at 17:32
  • Im very new to java (as you can all see) so how does it not make sense? – Manny A Mar 29 '18 at 17:38
  • when you write new Die(12); you are passing in a number (12) to the constructor but then you say that sides = 1 so the 12 is gone. – isaace Mar 29 '18 at 17:46

1 Answers1

1

I don't see any code prompting for user input. You might take a look at How can I get the user input in Java? to lead you toward prompting for user input. You'll need a Scanner and System.in after a print to console. You'll probably want to call Hero's printStats as well.

  • You are very right. My goal is to perform these actions: Call the round method using the heroes as input paramters, Print the stats of each hero, Call the round method using the heroes as input paramters, Print the stats of each hero (check), Compare the health of the two heroes, and Print the name of the hero with the most health and declare them the winner. – Manny A Mar 29 '18 at 17:27