简体   繁体   中英

How do I store a random variable and print it for a game?(java)

I'm trying to run the constructor with:

Main fight1 = new Main("Homeless Man", "Rusty Fork", 100, 110, 30);

I Want it to deal a random amount of damage (within a specified range), save that value for the next stage of combat, and then for the players health to drop and be stored respectively. I also want it to repeat until the enemy no loner has health. I can update the code below with the rest of the program if you want to try it yourself. The current output is:

Will you:
 -Attack (1)
 -Consume Item (2) 
 -Run (3)
1
Homeless Man hits you with a Rusty Fork, Dealing 54 damage!
You now have 903 HP left!
You hit Homeless Man With an attack using your Fists dealing 42 damage!
Homeless Man now has 60 HP left!

The actual program:

private static int playerMaxDam = 75;
private static int playerMinDam = 30;
private static int damageDelt;
private static int playerHP = 1000;
private static String currentWeapon = "Fists";

//default enemy variables
private static String enemyName;
private static String enemyItem;
private static int enemyHP;
private static int enemyMaxDam;
private static int enemyMinDam;
private static int damageTaken;

//enemy health

public int getDamageTaken() {
    
    Random rand = new Random();
    damageTaken = rand.nextInt(enemyMaxDam - enemyMinDam + 1) + enemyMinDam;
    return damageTaken;
}

public int getDamageDelt(){
    
    
    Random rand = new Random();
    damageDelt = rand.nextInt(playerMaxDam = playerMinDam + 1) + playerMinDam;
    return damageDelt;
}

public Main(String _enemyName, String _enemyItem, int _enemyHP, int _enemyMaxDam, int _enemyMinDam) {
    enemyName = _enemyName;
    enemyItem = _enemyItem; 
    enemyHP = _enemyHP;
    enemyMaxDam = _enemyMaxDam;
    enemyMinDam = _enemyMinDam;     
}

public String getPlayerWeapon() {
    
    
    String playerWeapon = null;
    String fists = "1";
    String bloody_Spoon = "0";
    
    if(fists.equals("1")) {
        playerMaxDam = 75;
        playerMinDam = 30;
        playerWeapon = "Fists";
    }else {
        if(bloody_Spoon.equals("1")) {
            playerMaxDam = 100;
            playerMinDam = 75;
            playerWeapon = "Bloody Spoon";
        }
    }
    
    return playerWeapon;
}

public void displayCombat() {
    //enemy
    int enemyHealth = (enemyHP - getDamageDelt());
    
    //player
    int playerHealth = (playerHP - getDamageTaken());
    
    //player armor
    int clothes = 1;
    int tux = 0;
    
    //player weapon
    String playerWeapon = currentWeapon;
    int fists = 1;
    int bloody_Spoon = 0;
    
    Scanner in = new Scanner(System.in);
    System.out.println("Will you:\n -Attack (1)\n -Consume Item (2) \n -Run (3)");
    String userInput = in.nextLine();
    if(userInput.equals("1")) {
        System.out.println(enemyName + " hits you with a " + enemyItem + ", Dealing " + getDamageDelt() + " damage!");
        System.out.println("You now have " + playerHealth + " HP left!");
        System.out.println("You hit " + enemyName + " With an attack using your " + playerWeapon + " dealing " + getDamageDelt() + " damage!");
        System.out.println(enemyName + " now has " + (enemyHP - getDamageDelt()) + " HP left!");
        
    }else {
        if(userInput.equals("3")) {
            System.out.println("You managed to escape the fight with " + playerHP + " left!");
        }
    }
}

In your displayCombat method, you never actually subtract the damage dealt to the enemy from his health. In this method you shouldn't initialize a new variable ( int enemyHealth = (enemyHP - getDamageDelt()); ).
Instead you should have something like enemyHP -= getDamageDelt(); , so that the damage is acutually subtracted from the enemies health.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM