【发布时间】:2015-11-01 16:20:32
【问题描述】:
所以,当一个类与另一个类相互作用时,我尝试编写代码。这是“Tim”的职业战士和“Max”的职业巫师。控制台应该说“现在最大马力.. 50”或“现在蒂姆的马力.. 50 马力”,但控制台说“现在最大马力.. 25”或“现在蒂姆的马力.. 25 马力”.. 75 - 25 = 50; 100-50=50;但是说 75 - 25 = 25; 100-50=25; 请帮忙:c
import java.util.Random;
public class MAXvsTIMvoidVersion {
public static void main(String[] args) {
warior Tim = new warior();
Tim.agility = 100;
Tim.attack = 25;
Tim.hp = 100;
Tim.Wariorlvl2 = true;
witcher Max = new witcher();
Max.mana = 150;
Max.attack = 50;
Max.hp = 75;
Max.Witcherlvl1 = true;
Random a = new Random();
int b = a.nextInt(2);
if (b == 0)
{
Tim.meleeAttack();
System.out.print("Now Max's hp.." + Max.hp);
}
else if (b == 1)
{
Max.magicAttack();
System.out.print("Now Tim's hp.." + Tim.hp);
}
}
}
class people
{
static int hp;
static int attack;
}
class witcher extends people
{
int mana;
boolean Witcherlvl1;
boolean Witcherlvl2;
void magicAttack()
{
warior.hp = warior.hp - witcher.attack;
mana = mana - 20;
}
}
class warior extends people
{
int agility;
boolean Wariorlvl1;
boolean Wariorlvl2;
void meleeAttack()
{
witcher.hp = witcher.hp - warior.attack;
agility = agility - 20;
}
}
【问题讨论】:
-
注意naming conventions是:类名以大写字母开头,变量名以小写字母开头。如果您使用这些约定,则代码对于普通程序员来说变得更容易阅读,并且该站点上的代码标记也可以更好地工作......
标签: java class if-statement