2015-10-25 78 views
0

這是代碼:我如果和else語句都不能正常工作

public class Champion 
{ 
    private String championName;   // champion name 

    private String championSummonersName; // Champion summoner's name 

    int    maxHitPoints1;   // Champion's max hit points 

    int    currentHitPoints1;  // Champion's current hit points 

    int    maxHitPoints2;   // summoner's max hit points 

    int    currentHitPoints2;  // summoner's current hit points 

    double   globalbuff = 1.1; 

    Champion(int MHP1, int CHP1, int MHP2, int CHP2) 
    { 
     maxHitPoints1 = 100; 
     currentHitPoints1 = 75; 
     maxHitPoints2 = 125; 
     currentHitPoints2 = 110; 
    } 

    public Champion(String name, String summoners) 
    { 
     championName = name; 
     championSummonersName = summoners; 
    } 

    public void setchampionName(String name) 
    { 
     championName = name; // store the champion name 
    } // end method setchampionName 

    // method to set the summoners name 
    public void setchampionSummonersName(String summoners) 
    { 
     championSummonersName = summoners; // store the summoners name 
    } 

    // method to retrieve the champion name 
    public String getchampionName() 
    { 
     return championName; 
    } // end method getchampionName 

    // method to retrieve the summoners name 
    public String getchampionSummonersName() 
    { 
     return championSummonersName; 
    } 

    public void setmaxHitPoints1(int MHP1) 
    { 
     if (getMHP1() < 0) 
     { 
      System.out.println(1); 
     } 
     else 
      maxHitPoints1 = MHP1; 

    } 

    public int getMHP1() 
    { 
     return maxHitPoints1; 

    } 

    public void setmaxHitPoints2(int MHP2) 
    { 
     if (getMHP2() < 0) 
     { 
      System.out.println(1); 
     } 
     else 
      maxHitPoints2 = MHP2; 

    } 

    public int getMHP2() 
    { 
     return maxHitPoints2; 

    } 

    public void setcurrentHitPoints1(int CHP1) 
    { 
     if (CHP1 <= 0) 
     { 
      currentHitPoints1 = 0; 
      System.out.println("Champion is dead"); 
     } 
     else 
      currentHitPoints1 = CHP1; 

    } 

    public int getCHP1() 
    { 
     return this.currentHitPoints1; 

    } 

    public void setcurrentHitPoints2(int CHP2) 
    { 
     if (CHP2 <= 0) 
     { 
      currentHitPoints2 = 0; 
      System.out.println("summoner is dead"); 
     } 
     else 
      currentHitPoints2 = CHP2; 

    } 

    public int getCHP2() 
    { 
     return currentHitPoints2; 
    } 

    public void displayMessage() 
    { 
     // this statement calls getCharacterName to get the 
     // name of the course this DnDCharacterSheet represents 

     System.out.printf("Champion is %s!\n", getchampionName()); 
     System.out.printf("Cain's max hit points are: " + getMHP1()); 
     System.out.printf("\nCain's current hit points are: " + getCHP1()); 
     /* write code to output the character's player name */ 
     System.out.printf("\nSummoner is %s!", getchampionSummonersName()); 
     System.out.printf("\nAble's max hit points are: " + getMHP2()); 
     System.out.printf("\nAble's current hit points are: " + getCHP2()); 
     System.out.printf("\n\nGlobal buff by 10 percent to max and current hit points\n"); 
     System.out.printf("\nCain's buff\nmax hit points: " + globalbuff * getMHP1()); 
     System.out.printf("\ncurrent hit points: " + globalbuff * getCHP1()); 
     System.out.printf("\nAble's buff\nmax hit points: " + globalbuff * getMHP2()); 
     System.out.printf("\ncurrent hit points! " + globalbuff * getCHP2()); 
     System.out.printf("\n\nRKO OUTTA NOWHERE!!!! CAUSING 500 DAMAGE TO EVERYTHING\n"); 

     System.out.printf("\nCain after RKO\nmax hit points: " + globalbuff * getMHP1()); 
     System.out.printf("\n"); 
     System.out.printf("current hit points: "); 
     System.out.println(getCHP1() - 500); 
     System.out.printf("\n\n"); 
     System.out.printf("Able after RKO\nmax hit points: " + globalbuff * getMHP2()); 
     System.out.printf("\n"); 
     System.out.printf("current hit points: "); 
     System.out.println(getCHP2() - 500); 

    } 
} 

所以當生命值降到0,我希望它簡單地說「0」,而是它輸出爲負數或者雖然我有我的if語句。它編譯正確它只是不正確的輸出,我很好奇,爲什麼?

回答

0

首先你應該看看一些面向對象編程的編程教程。你創建一個Champion類,但從不使用它。你創建了一堆你永遠不會使用的方法,大多數只是返回一個可以直接在類中訪問的字段。

現在你的問題:你永遠不會使用setcurrentHitPointsX(int CHP1),因此生命值永遠不會被修改。

您打印爲負值,因爲你打印出當前生命值減去500

此:

System.out.println(getCHP1() - 500); 

技術上是這樣的:

System.out.println(75 - 500); 

導致負值:-425

要解決這個問題,你可以執行y使用你的方法:

setcurrentHitPoints1(-500); 
System.out.println(getCHP1()); // prints 0 

但是從你的文字冠軍實際上是接收500點傷害,所以你應該添加當前生命值計算進去。

+0

我使用的方法抱歉忘記提及這是在Champion.java下使用,然後我不得不創建ChampionTest.java一切工作在測試中。 –

+0

@RyanJacobberger你的問題解決了嗎? –

+0

小指漩渦謝謝我想通了! –