2015-10-13 48 views
-1

我試圖比較兩個對象的實例在java中比較兩個實例變量

a。創建一個名爲Cyclops的超級英雄

Superhero Cyclops = new Superhero("Cyclops"); 

b。創建一個名爲巨像的超級英雄。龐然大物具有40

Superhero Colossus = new SuperHero("Colossus", 40); 

稱爲水蚤的對象的第一個實例具有10默認值的強度,和所述第二對象已分配的40的值,這是代碼內的強度值。

我該如何制定一個戰鬥方法,考慮到他們的優勢,然後確定贏家?

的超級英雄類的代碼顯示如下

public class Superhero{ 

    //private instance variables declared below 
    //name of the super hero 
    private String name; 
    //name of the strength variable 
    private int strength; 
    //The variable for the powerUp method 
    private int powerUp; 
    //variable to store the power of the power up 
    private int storePower; 



    //getter and setter methods. 
    public int getStrength(){ 
     return strength; 
    } 




    /* 
    This method takes in an integer paremeter and based on that it does 
    some calculations. 

    There are four possible integer values you can pass in: 100, 75, 50 and 25. 

    If 100 has been passed in the storePower variable is set to 40 which would be 
    a power-up of 40, if 75 then power-up of 30, if 50 then a power-up of 20, 
    if 25 then a power-up of 10 and if the right ammount is not specified a power-up 
    of 0 will be assigned. 

    */ 
    public int powerUp(int powerUp){ 
     this.powerUp = powerUp; 
     if(powerUp == 100){ 
      storePower = 40; 
     }else if(powerUp == 75){ 
      storePower = 30; 
     }else if(powerUp == 50){ 
      storePower = 20; 
     }else if(powerUp == 25){ 
      storePower = 10; 
     }else{ 
      storePower = 0; 
     } 
     strength = strength + storePower; 
     System.out.println("After receiving a power-up: " + name + " has a Strength of: " + strength); 
     return strength; 

    } 

    public void fight(){ 


    } 


    //constructor for if the player wanted to specify the name and the strength 
    public Superhero(String name, int strength){ 
     this.name = name; 
     this.strength = strength; 

     System.out.println(name + " " + strength); 
    } 



    //if the user doesn't enter the strength as it is optional 
    //this constructor below will run and set the default 
    public Superhero(String name){ 
     this.name = name; 
     strength = 10; 
     System.out.println(name + " " + strength); 

    } 

} 

,正如你可以看到公共無效的戰鬥,是空的,因爲這是我想創建方法。

也顯示下方

public class Fight{ 
    public static void main(String[] args){ 

     // a. Create a Superhero named Cyclops 
     Superhero Cyclops = new Superhero("Cyclops"); 

     //b. Create a Superhero named Colossus. Colossus has a strength of 40 
     Superhero Colossus = new SuperHero("Colossus", 40); 

     Cyclops.fight(Cyclops.getStrength(), Colossus.getStrength()); 

     //d. Give Cyclops a powerUp of 100 
     Cyclops.powerUp(100); 
    } 
} 
+0

事實上,你在加強戰鬥後的獨眼巨人在任何重要的戰鬥? – ThisClark

+0

[爲Java遊戲生成攻擊]可能的副本(http://stackoverflow.com/questions/29182412/generating-attack-for-java-game)。 –

+0

是的,加電是爲獨眼巨人增加額外的力量加值,所以如果用戶輸入100加電,獨眼巨人將獲得40力量加值 – user5438578

回答

2

戰鬥類的主要方法可以通過參數傳遞一個英雄,你有其他方法,並返回戰鬥的勝利者:

public SuperHero fight(SuperHero opponent) { 
     if (this.getStrength() > opponent.getStrength()) 
      return this 
     return opponent; 
} 

所以,你可以對主電話:

SuperHero winner = cyclope.fight(colossus); 
System.out.println("The winner is: " + winner); 

這個問題可以幫助你理解浩w想想問題。你在這裏有一個超級英雄班,你想讓超級英雄戰鬥,如你所知,一場戰鬥有一個勝利者,所以這個功能的返回可能是戰鬥的勝利者。在正在創建的實例的行爲上,通過this關鍵字\變量訪問的實例的行爲,因此您只需要英雄會對抗的對手this,創建參數SuperHero opponent是一個好方法。比較實力值,你會發現獲勝者並將其返回。你可以改進這些東西並添加更多的屬性進行比較,同樣,當SuperHeros具有相同的強度時定義一個行爲,如果你可以做一個隨機選擇,那將是很棒的。

再一個建議,看看我的變量的名稱,他們以小寫字母開頭,這是一個Java約定,你應該關心它,它可以幫助你閱讀你的代碼。

+0

有一些錯誤,我與該代碼 – user5438578

+0

獨眼巨人10個 巨像得到40 獲獎者是:超級英雄@ 6d06d69c 接收電後:獨眼巨人具有的強度:50 – user5438578

+0

爲什麼是錯誤發生:獲獎者是:超級英雄@ 6d06d69c – user5438578