java吧 关注:1,288,209贴子:12,816,831
  • 3回复贴,共1

程序问题求助

只看楼主收藏回复

package com.mirror.ts;
public class Player {
int atk;
int def;
int blood;
public Player(int c){
this.blood = c;
this.atk = (int)(100*(Math.random()));
this.def = (int)(30*(Math.random())); }
public void Show(Player a ,Player b){
if(a.blood < 0){
a.blood = 0;
}else if(b.blood < 0){
b.blood = 0;
}
System.out.println("成员a的血量,攻击,防守为:"+ a.blood + ","+ a.atk + "," +a.def);
System.out.println("成员b的血量,攻击,防守为:"+ b.blood + ","+ b.atk + "," +b.def);
}
public void Attack(Player a,Player b){
if(a.atk > b.def){
b.blood -= a.atk - b.def;
} else{
a.blood += a.atk - b.def;
}
}
}



1楼2011-10-12 23:30回复
    package com.mirror.ts;
    public class Main {
    public static void main(String args[]){
    boolean flag;
    Player a = new Player(1000);
    Player b = new Player(1000);
    new Player.Show(a,b);
    flag = true;
    while(a.blood>0&&b.blood>0){
    if(flag){ Attack(a,b);
    Show(a,b);
    flag=false;
    continue;
    }
    if(!flag){
    Attack(b,a);
    Show(a,b);
    flag=true;
    }
    }
    if(a.blood == 0){
    System.out.println("成员b胜利!!!!!!");
    }else if(b.blood == 0){
    System.out.println("成员a胜利!!!!!!");
    }
    }
    }
    


    2楼2011-10-12 23:32
    回复
      2025-11-21 04:33:55
      广告
      不感兴趣
      开通SVIP免广告
      这个程序有两个类第一个类中的都是非静态的方法,第二个类的main调用它的时候会出错,我知道,只要把第一个类的所有方法都改成Static的就可以,但是如果不改的可以在main的方法里掉能用吗??


      3楼2011-10-12 23:34
      回复
        可以通过“对象名.方法();”调用


        4楼2011-10-13 09:49
        回复