2015-10-12 163 views
-4

對於我來說明我試圖讓此代碼執行的最佳方式是複製並粘貼給定的賦值。Java對象和構造函數/方法

我自己編寫了這個東西,但是我遇到的問題是兩個變量似乎根本沒有加起來,但是當我測試它的代碼時它仍然有效。

兩個變量,我試圖去加起來是storePower和力量

創建一個類來表示一個超級英雄。每個超級英雄都有一個名字。每個超級英雄也可以有一個實力,但這應該是可選的。如果一個超級英雄不具備實力,其默認實力應該是10

每個英雄可以接收通電,因此他們的實力提高了一個特定的編輯量

公共類超級英雄{

//private instance variables declared below. 

//name of the super hero 
private String name; 
//name of power up variable 
private int powerUp; 
//name of the strength variable 
private int strength; 
private int storePower; 







/* 
Power ups work in a way that if the powerUp is greater then or equal to 10 then add 5 to strength 
if the powerUp is greater than or equal to 5 then add 2 to the strength 
if it is less than 5 then add 1 to the strength. 
*/ 

public void powerUp(int powerUp){ 
this.powerUp = powerUp; 
    if(powerUp >= 10){ 
     storePower = 5; 
     strength = storePower + strength; 
     //this system out was to test if this bit of code is working. 
     System.out.println("Power up is set to 10 or higher!" + " The power is: " + strength); 
    }else if(powerUp >= 5){ 
     storePower = 2; 
     strength = storePower + strength; 
    }else if (powerUp < 5){ 
     storePower = 1; 
     strength = storePower + strength; 
    }else{ 
     System.out.println("Something is not right.."); 
    } 


} 

//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; 
    this.strength = 10 + storePower; 
    System.out.println(name + " " + strength); 
} 

} 撲滅類下面

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

    //creating a new instance of the Superhero object 
    Superhero cyclops = new Superhero("cyclops"); 



    cyclops.powerUp(10); 
} 

}

在命令行中運行時,輸出爲:

獨眼龍10
功率高達設置爲10或更高!力量是:5

我想獲得的輸出是15,因爲上電設置爲高於10,因此if語句將其設置爲5,但我不能讓這些加起來

+0

_I不能讓這些加起來不是問題。爲什麼你不能讓他們加起來?你的調試顯示了什麼? –

+0

我曾嘗試將兩個變量加在一起,但它不起作用 – user3135672

+0

this.strength = strength + storePower; – user3135672

回答

0

這樣修改你的代碼

public void powerUp(int powerUp){ 
this.powerUp = powerUp; 
    if(powerUp >= 10){ 
     storePower = 5; 
      strength = storePower+powerUp 
     //this system out was to test if this bit of code is working. 
     System.out.println("Power up is set to 10 or higher!" + " The power is: " + strength ); 
    }else if(powerUp >= 5 & powerUp <10){ 
     storePower = 2; 
     strength = powerUp+StorePower 
System.out.println("Power up is set to 5 or greater but less than 10!" + " The power is: " + strength ); 
    }else if (powerUp < 5){ 
     storePower = 1; 
     Strength = storePower + powerUp; 
System.out.println("Power up is set to less than 5!" + " The power is: " + strength ); 
    }else{ 
     System.out.println("Something is not right.."); 
    } 

} 
+0

該代碼帶來了不少錯誤無法使其工作 – user3135672

+0

即使在修復它仍然保持獨眼巨人的錯誤10 – user3135672

+0

您能否請告訴我們你需要什麼輸出? ,這個代碼應該打印。加電設置爲10或更高!力量是15 – developer2015