Using methods from an outside class to get a specific variable from that method

java

Solution

It looks like what you really want is a constructor for your `Enemies` class, rather than a `void` method:

public Enemies(int choice){
    switch(choice)
    {
    case 1: 
        this.enemyName = "Sentient Shoelace";
        this.enemyHealth = 100.0;
        this.enemyAttackDamage = 1;
        this.enemyDefenseLevel = 1;
        this.hardLevel = 2;
        this.lootDropped = 100;
        break;
    case 2:
        this.enemyName = "Slimey Fartbox";
        this.enemyHealth = 100.0;
        this.enemyAttackDamage = 2;
        this.enemyDefenseLevel = 1;
        this.hardLevel = 3;
        this.lootDropped = 100;
        break;
    default: System.out.println("Mess up in the enemy selection process");
    }
}

Now you can change your `fight()` method as follows:

private void fight()
{
    //I'm using a rand.nextInt() to get enemychoice
    Enemies enemy = new Enemies(enemyChoice);
    System.out.println("You run into a "  + enemy.enemyName);
}

There are still some stylistic issues you might like to ponder on:

- You really want it to be called `Enemy` rather than `Enemies`.

- You could think about using an `enum` if you just have two possibilities for an `Enemy`.

- You might also think about having another constructor that takes six parameters, one for each thing you're setting. This could be a `private` constructor if you like, and that could be invoked by the `public` constructor that takes the `int choice`.

Problem

I'm writing a wee text based questing game in Java to get the hang of the language a bit better. I'm trying to create a method that can be called from another class. The method I want to call contains repeated variables and I want the correct variable to show up when I use a `switch` statement. ``` public class Enemies { String enemyName = "Generic Enemy";//initialised the enemy stats so something is called if the switch failed double enemyHealth = 110.0; int enemyAttackDamage = 10; int enemyDefenseLevel = 1; int hardLevel = 1; int lootDropped = 50; public void enemyDecided(int choice){ switch(choice) { case 1: this.enemyName = "Sentient Shoelace"; this.enemyHealth = 100.0; this.enemyAttackDamage = 1; this.enemyDefenseLevel = 1; this.hardLevel = 2; this.lootDropped = 100; break; case 2: this.enemyName = "Slimey Fartbox"; this.enemyHealth = 100.0; this.enemyAttackDamage = 2; this.enemyDefenseLevel = 1; this.hardLevel = 3; this.lootDropped = 100; break; default: System.out.println("Mess up in the enemy selection process"); } } } ``` I'm trying to call this from another class by ``` private void fight() { //I'm using a rand.nextInt() to get enemychoice Enemies enemyToFight = new Enemies(); enemy = enemyToFight.enemyDecided(enemyChoice);// System.out.println("You run into a " + enemy.enemyName); } ``` My `fight()` method doesn't work at all. And for good reason. What I have wrote is pure nonsense, though I'm at my wits' end and the edge of my meager knowledge. I'm sure anyone with eyes can see that I'm trying to build a pseudo `struct`. As a recent convert from C, I'm still learning how to do things better. If this is all a terribly misguided idea to do in Java then could you point me in the right direction. Though a chance to save it would make me very happy indeed. But I would like to try and get this working right if it's possible, so any help is appreciated.

Original source