标签:ann 输入 hit 数据 name turn static setname string
在同一个包中的Player类
package work;
public class Player {
private String name;
private int hp;
private int attack;
public Player(String name,int hp,int attack){//构造函数,初始化值
this.name=name;
this.hp=hp;
this.attack=attack;
}
public void setName(String name) {//获取值
this.name=name;
}
public String getName(){
return name;
}
public void setHp(int hp){
this.hp=hp;
}
public int getHp(){
return hp;
}
public void setAttack(int attack){
this.attack=attack;
}
public int getAttack(){
return attack;
}/**
*
*攻击行为、
*player 为攻击对象
***/
public void hit(Player player){
//被打的对象(血量=血量-本人攻击力)
player.hp=player.hp-this.attack;
if(player.hp<0){
player.hp=0;
System.out.println(this.name+"攻击了"+player.name+"打出了"+attack+"点伤害."+player.name+"还剩下多沙"+player.hp+"血量");
}
}
}
在同一个包中另一个Game类
package work;
import java.util.Scanner;
/**
* @author 游戏
*
*/
public class Game {
Scanner sc=new Scanner(System.in);
// 从控制台接受数据并且封装成一个玩家对象
public Player enter(String player) {//封装玩家信息
System.out.println("输入"+player+"姓名");
String name=sc.next();
System.out.println("输入"+player+"血量");
int hp=sc.nextInt();
System.out.println("输入"+player+"攻击力");
int attack=sc.nextInt();
Player backPlayer=new Player(name, hp,attack);
return backPlayer;
}
public void start() {
//从封装好的函数中输入玩家的信息
Player player1=this.enter("玩家一");
Player player2=this.enter("玩家二");
boolean go=true;
while(go){
//玩家一打了玩家二
player1.hit(player2);
if(player2.getHp()<0){
System.out.println(player1.getName()+"ko了"+player2.getName()+"游戏结束!fighting!!!!!!!!!!");
break;
}
//玩家二打了玩家一
player2.hit(player1);
if(player1.getHp()<0){
System.out.println(player2.getName()+"ko了"+player1.getName()+"游戏结束!fighting!!!!!!!!!!");
go=false;
}
}
}
public static void main(String[] args) {
Game game=new Game();
game.start();
}
}
标签:ann 输入 hit 数据 name turn static setname string
原文地址:http://www.cnblogs.com/lqh969696/p/7761836.html