Java Learning 2 Fighting code writing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.