gpt4 book ai didi

java - 如何从同一类的另一个对象调用变量

转载 作者:行者123 更新时间:2023-12-01 18:14:32 24 4
gpt4 key购买 nike

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;
}
}

因此,在最后一个返回语句中,我试图获取另一个对象(正在被攻击的角色)的防御变量。我试图根据该角色的强度统计数据获取随机数,然后创建伤害统计数据。之后我会造成伤害-防御(来自另一个角色)。有谁知道我为此使用什么代码? PS:请不要畏缩,这是我们这些天在学校学到的东西。

最佳答案

根据您的描述,您希望您的方法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;
}

删除void方法Character,如果它是一个构造函数,那么您已经有一个具有相同签名的构造函数。

关于java - 如何从同一类的另一个对象调用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60400037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com