samedi 12 novembre 2016

use an int Variable for range to get random number

I am still fairly new to java. I want to make a game with 3 character types that have different stats. I am using int values for each type so that their attack value is a range instead of just being a constant value. Since each character has a different range, I want to substitute an int value instead of an actual number for the method to get a random number. Here is my code. package battleme;

import java.util.Random;

/**
*
* @author Kitten
*/
class Character {


 String name;
int life;
int playerAttacklow;
int playerAttackhigh;
int playerDefense;
int playerLevel;
int currentXP;
int currentGold;



   public Character(String name, int life, int playerAttacklow, 
        int playerAttachhigh, int playerDefense, 
        int playerLevel, int currentXP, int currentGold) {
}



public String getName() {
    return name;
}

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

public int getLife() {
    return life;
}

public void setLife(int life) {
    this.life = life;
}

public int getPlayerAttacklow() {
    return playerAttacklow;
}

public void setPlayerAttacklow(int playerAttacklow) {
    this.playerAttacklow = playerAttacklow;
}

public int getPlayerAttackhigh() {
    return playerAttackhigh;
}

public  void setPlayerAttackhigh(int playerAttackhigh) {
    this.playerAttackhigh = playerAttackhigh;
}

public int getPlayerDefense() {
    return playerDefense;
}

public void setPlayerDefense(int playerDefense) {
    this.playerDefense = playerDefense;
}

public int getPlayerLevel() {
    return playerLevel;
}

public void setPlayerLevel(int playerLevel) {
    this.playerLevel = playerLevel;
}

public int getCurrentXP() {
    return currentXP;
}

public void setCurrentXP(int currentXP) {
    this.currentXP = currentXP;
}

public int getCurrentGold() {
    return currentGold;
}

public void setCurrentGold(int currentGold) {
 this.currentGold = currentGold;
}
//the problem child
int ActualAttackGen(int playerAttackhigh, int playerAttacklow) {
    Random rn = new Random();
    int randomNum;

    randomNum= rn.nextInt((playerAttackhigh-playerAttacklow) + 1)+ playerAttacklow ;


            return randomNum ;
}

package battleme;



public class BattleMe {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
   Character Warrior = new Character("Warrior", 30, 2, 10, 3, 1, 1, 15);
   Character Rouge = new Character("Rouge", 25, 3, 6, 2, 1, 1, 15);
   Character Mage = new Character("Mage", 18, 2, 8, 1, 1, 1, 15);        

   // trying to run the problem child
   System.out.println(Warrior.ActualAttackGen(Warrior.playerAttackhigh,Warrior.playerAttacklow));


}

}

Whenever I try to run this, I always get an value of 0. Please help!




Aucun commentaire:

Enregistrer un commentaire