[英]How to call upon a variable from another object of the same class
import java.util.Random;
public class Character
{
// instance variables - vervang deze door jouw variabelen
private String _name;
private double _level;
private double _strenght;
private double _speed;
private double _defense;
private double _currenthp;
private double _maxhp;
/**
* Voorbeeld van een method - schrijf hier jouw comment
*
* @param y deze method krijgt deze parameter mee in de aanroep
* @return deze method geeft de som van x en y terug
*/
public Character()
{
}
public Character(String name,double level)
{
// schrijf hier jouw code
_name = name;
_level = level;
Random rand = new Random();
//level voor het berekenen van skills
int lvl = (int)level;
//stenght en speed parameter voor random
int stsplvl = 12*lvl - 8*lvl;
//defense parameter voor random
int deflvl = 6*lvl - 4*lvl;
//hp parameter voor random
int hplvl = 30*lvl - 20*lvl;
//Strenght berekenen
double st = (double)rand.nextInt(stsplvl);
st = st + 8*lvl;
_strenght = st;
//Speed berekenen
double sp = (double)rand.nextInt(stsplvl);
sp = sp + 8*lvl;
_speed = sp;
//Defense berekenen
double def = (double)rand.nextInt(deflvl);
def = def + 4*lvl;
_defense = def;
//Hp berekenen
double hp = (double)rand.nextInt(hplvl);
hp = hp + 20*lvl;
_currenthp = hp;
_maxhp = hp;
}
public void Character(String name,double level)
{
_name = name;
_level = level;
}
public void ploth()
{
System.out.println(_name + ": " + _currenthp + " / " + _maxhp + " HP");
}
public int attack(Character _other)
{
int defe = super (double._other _defense);
Random rando = new Random();
//_strenght is een double
int damcalc = (int)(1.2 * _strenght - 0.8 * _strenght);
int netdam = rando.nextInt(damcalc);
int totdam = (int)(netdam + 0.8*_strenght);
int enddam = totdam - defe;
}
}
So in the last return statement I am trying to get the defense variable of the other object (character that is being attacked).所以在最后一个 return 语句中,我试图获取另一个对象(被攻击的角色)的防御变量。 I am trying to take a random number based on the strength stat of this character and then to create a damage stat.我试图根据该角色的力量统计数据获取一个随机数,然后创建一个伤害统计数据。 After that I'd do damage - defense (from the other character).之后我会造成伤害 - 防御(来自另一个角色)。 Does anyone know what code I use for this?有谁知道我为此使用什么代码? PS:Please don't cringe this is the stuff we learn at school these days. PS:请不要畏缩这是我们这些天在学校学到的东西。
From your description you want your method attack
return enddam
like below:根据您的描述,您希望您的方法attack
返回enddam
如下所示:
public int attack(Character _other)
{
int defe = (int)_other._defense;
Random rando = new Random();
//_strenght is een double
int damcalc = (int)(1.2 * _strenght - 0.8 * _strenght);
int netdam = rando.nextInt(damcalc);
int totdam = (int)(netdam + 0.8*_strenght);
int enddam = totdam - defe;
return enddam;
}
Delete the void
method Character
, if it is a constructor you already have one with the same signature.删除void
方法Character
,如果它是构造函数,则您已经拥有具有相同签名的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.