Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Java Online

public class HelloWorld{

    static class Player{
        String name;
        int hp;
        int atk;
        
        public Player(String name, int hp, int atk){
            this.name = name;
            this.hp = hp;
            this.atk = atk;
        }
        public void attack(Enemy enemy){
            System.out.println("Enemy Attack!");
            enemy.hp -= this.atk;
            System.out.println("Enemy hp :"+enemy.hp);
        }
        public boolean isLive(){
            if(hp <= 0){
                return false;
            }else{
                return true;
            }
        }
    }
    
    static class Enemy{
        String name;
        int hp;
        int atk;
        
        public Enemy(String name, int hp, int atk){
            this.name = name;
            this.hp = hp;
            this.atk = atk;
        }
        public void attack(Player player){
            System.out.println("Player Attack!");
            player.hp -= this.atk;
            System.out.println("Player hp :"+player.hp);
        }
        public boolean isLive(){
            if(hp <= 0){
                return false;
            }else{
                return true;
            }
        }
    }

     public static void main(String []args){
        
        Player player = new Player("gampari", 100, 12);
        Enemy enemy = new Enemy("Orc", 80, 5);
        
        while(player.isLive() && enemy.isLive()){
            player.attack(enemy);
            if(!enemy.isLive()) break;
            enemy.attack(player);
        }
        
        if(player.isLive()){
            System.out.println("player win");
        }else{
            System.out.println("enemy win");
        }
     }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.