The player class in the same package
Package work;
public class Player {
private String name;
private int HP;
private int attack;
Public Player (String name,int hp,int attack) {//constructor, initialization value
This.name=name;
THIS.HP=HP;
This.attack=attack;
}
public void SetName (String name) {//Get value
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;
}/**
*
* Attack Behavior,
*player as Attack object
***/
public void hit (player player) {
The object being beaten (amount of blood = amount of blood-my attack)
Player.hp=player.hp-this.attack;
if (player.hp<0) {
Player.hp=0;
System.out.println (this.name+ "attacked" +player.name+ "hit" +attack+ "point damage." +player.name+ "There is still more sand" +player.hp+ "blood volume");
}
}
}
Another game class in the same package
Package work;
Import Java.util.Scanner;
/**
* @author Games
*
*/
public class Game {
Scanner sc=new Scanner (system.in);
Accepting data from the console and encapsulating it as a player object
Public player Enter (String player) {//encapsulates player information
System.out.println ("Enter" +player+ "name");
String Name=sc.next ();
SYSTEM.OUT.PRINTLN ("Input" +player+ "blood volume");
int Hp=sc.nextint ();
SYSTEM.OUT.PRINTLN ("Input" +player+ "attack");
int Attack=sc.nextint ();
Player Backplayer=new player (name, hp,attack);
return backplayer;
}
public void Start () {
Enter the player's information from the encapsulated function
Player Player1=this.enter ("Player One");
Player Player2=this.enter ("Player II");
Boolean go=true;
while (GO) {
Player a dozen players two
Player1.hit (PLAYER2);
if (PLAYER2.GETHP () <0) {
System.out.println (Player1.getname () + "Ko" +player2.getname () + "Game Over!fighting!!!!!!!!!! ");
Break
}
Player two hit the player one
Player2.hit (Player1);
if (PLAYER1.GETHP () <0) {
System.out.println (Player2.getname () + "Ko" +player1.getname () + "Game Over!fighting!!!!!!!!!! ");
Go=false;
}
}
}
public static void Main (string[] args) {
Game Game=new game ();
Game.start ();
}
}
Java Learning 2 Fighting code writing