(1)编写一个用于表示战斗能力的接口Fightable,该接口包含:整型常量MAX;方法void win(),用于描述战斗者获胜后的行为;方法int injure(int x),用于描述战斗者受伤后的行为。
public class Month{
public static void main(String[] args) {
combat combat=new combat();
combat.win();
combat.injure(0);
}
}
interface Fightable{
public final static int MAX=120;
public abstract void win();
public abstract int injure(int x);
}
class combat implements Fightable{
String player1="西门吹雪";
String player2="东门飘雨";
int life1=MAX;
int life2=MAX;
int life;
public void win() {
while ((life1!=0||life2!=0)&&(life1>0&&life2>0)) {
int x=(int) (Math.random()*10+1);
int y=(int) (Math.random()*10+1);
if (x>y) {
System.out.println(player1+"点数为:"+x+" > "+y);
life2=life2-30;
System.out.println(player2+"剩余生命值:"+life2);
}else if(x<y){
System.out.println(player2+"点数为:"+y+" > "+x);
life1=life1-30;
System.out.println(player1+"剩余生命值:"+life1);
}else {
System.out.println("平局,重新开始");
}
}
if (life1==0) {
System.out.println(player2+"获胜");
}
if (life2==0) {
System.out.println(player1+"获胜");
}
}
public int injure(int x) {
if (life1==0) {
System.out.println(player1+"说:留着青山在,不怕没柴烧");
}
if (life2==0) {
System.out.println(player1+"说:36计,走位上册");
}
return life;
}
public class Month{
public static void main(String[] args) {
combat combat=new combat();
combat.win();
combat.injure(0);
}
}
interface Fightable{
public final static int MAX=120;
public abstract void win();
public abstract int injure(int x);
}
class combat implements Fightable{
String player1="西门吹雪";
String player2="东门飘雨";
int life1=MAX;
int life2=MAX;
int life;
public void win() {
while ((life1!=0||life2!=0)&&(life1>0&&life2>0)) {
int x=(int) (Math.random()*10+1);
int y=(int) (Math.random()*10+1);
if (x>y) {
System.out.println(player1+"点数为:"+x+" > "+y);
life2=life2-30;
System.out.println(player2+"剩余生命值:"+life2);
}else if(x<y){
System.out.println(player2+"点数为:"+y+" > "+x);
life1=life1-30;
System.out.println(player1+"剩余生命值:"+life1);
}else {
System.out.println("平局,重新开始");
}
}
if (life1==0) {
System.out.println(player2+"获胜");
}
if (life2==0) {
System.out.println(player1+"获胜");
}
}
public int injure(int x) {
if (life1==0) {
System.out.println(player1+"说:留着青山在,不怕没柴烧");
}
if (life2==0) {
System.out.println(player1+"说:36计,走位上册");
}
return life;
}
}