2015-09-05 18 views
-1

我的目標是讓2個不同的對象相互對抗,並顯示結果。我的問題是我無法弄清楚如何正確設置攻擊和健康,以便它實際更新它所支持的方式。試圖在Java中使用2個對象

import java.util.Random; 
import java.util.Scanner; 

/** 
* 
* @author Brenton 
*/ 
public class Fighter { 

    private String name; 
    private int attack; 
    private int level = 1; 
    private int health = 50; 
    private boolean isAlive = true; 
    private Fighter fighterTwo; 

    public String getName() { 
     return this.name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAttack() { 
     Random generator = new Random(); 
     attack = generator.nextInt(10) * level + 1; 
     return attack; 
    } 

    public void setAttack(int attack) { 
     this.attack = attack; 
    } 

    public int getLevel() { 
     if(level >= 60) 
     { 
      level = 60; 
     } 
     return this.level; 
    } 

    public void setLevel(int level) { 
     this.level = level; 
    } 

    public int getHealth() { 
     if(this.health <= 0) 
     { 
      this.health = 0; 
     } 
     return this.health; 
    } 

    public void setHealth(int health) { 
     this.health = health; 
    } 

    public boolean isAlive() { 
     if(this.health <= 0) 
     { 
      this.isAlive = false; 
     } 
     return this.isAlive; 
    } 

    public static String getWelcome() { 
     String welcome = "Hello and welcome to FightClub, do you wish to fight, yes or no? "; 
     return welcome; 
    } 

    public String getPunch(Fighter fighterTwo) { 
     this.fighterTwo = fighterTwo; 
     String hit = "You choose to punch the other fighter and dealt " + getAttack() + " damage, your opponent now has " + this.decreaseHitPoints(fighterTwo) + " health remaining"; 
     return hit; 
    } 

    public int decreaseHitPoints(Fighter fighterTwo) { 
     this.fighterTwo = fighterTwo; 
     int health = fighterTwo.getHealth(); 
     int attack = getAttack(); 
     health = health - attack; 
     return health; 
    } 

    public static String invalidInput() { 
     String invalid = "I am sorry that is not a valid input option "; 
     return invalid; 
    } 

    public void getWinner(Fighter fighterTwo) { 
     this.fighterTwo = fighterTwo; 
     if(this.isAlive() == false && fighterTwo.isAlive() == false) 
     { 
      System.out.println("Both fighters have fallen heroically"); 
     } 
     else if(this.isAlive() == true && fighterTwo.isAlive() == false) 
     { 
      System.out.println(this.getName() + " is victorious! "); 
     } 
     else if(this.isAlive() == false && fighterTwo.isAlive() == true) 
     { 
      System.out.println(fighterTwo + " is victorious! "); 
     } 
     else 
     { 
      System.out.println("ERROR ERROR ERROR"); 
     }  
    } 

    public static void main(String[] args) { 

     Scanner in = new Scanner(System.in); 

     Fighter a = new Warrior(); 
     Fighter b = new Dragon(); 

     System.out.print(getWelcome());  
     while(in.hasNextLine()) 
     { 
      switch(in.nextLine()) 
      { 
       case "no": 
        System.out.println("Wow, you are not even gonna try, you have lost!"); 
        break; 
       case "yes": 
        System.out.println("Let the fight begin! "); 
        while(a.isAlive() && b.isAlive()) 
        { 
         System.out.println("Do you want to punch, kick, or headbutt the other fighter? "); 
         switch(in.nextLine()) 
         { 
          case "punch": 
           System.out.println(a.getPunch(b)); 
           break; 
          /*case "kick": 
           System.out.println(a.getKick(b)); 
           break; 
          case "headbutt": 
           System.out.println(a.getHeadbutt(b)); 
           break;*/ 
          default : 
           System.out.println(invalidInput()); 
           break; 
         } 
        } 
       default: 
        System.out.println(invalidInput()); 
        break; 
      }//end of first switch statement 
     }//end of first while loop 
    }//end of main 
} 
+0

你預期會發生什麼?究竟發生了什麼? –

+0

我預計對象1的攻擊方法會攻擊對象2並通過攻擊量減少對象2的健康狀況,實際發生的是攻擊是否正常運行,但是新的運行狀況計算不正確。 – TheDetailer

+0

你爲什麼投入如此多的獲得者?這使得代碼非常混亂,試圖在每種方法中保留更多相關的東西。 –

回答

1

您正在計算正確的攻擊。你只是沒有更新其他戰鬥機的狀態。

在你main()方法您啓動與

System.out.println(a.getPunch(b)); 

這只是罰款的攻擊。 ab處投擲拳,然後打印出從getPunch()返回的生命值。所以讓我們深入探究getPunch()以嘗試找出問題。

getPunch()你最終調用

this.decreaseHitPoints(fighterTwo) 

而構建的回報String。這似乎是正確的方法,所以decreaseHitPoints()有問題嗎?

public int decreaseHitPoints(Fighter fighterTwo) { 
    this.fighterTwo = fighterTwo; 
    int health = fighterTwo.getHealth(); 
    int attack = getAttack(); 
    health = health - attack; 
    return health; 
} 

您分配fighterTwo參數傳送給fighterTwo領域。不知道爲什麼,但這沒有錯本身。然後你將他的健康變成一個名爲health的局部變量。然後你將攻擊轉移到名爲attack的局部變量中。然後你從health減去attack,然後返回計算的值。但你永遠不會更新fighterTwo上的健康值!所以你只需要在你的程序中多出一行:在你的退貨聲明之前,插入

fighterTwo.setHealth(health);