2016-07-06 1 views
1

매개 변수 값을 전달하고 편집 한 다음 반환하면 매개 변수 값이 변경되지 않는 것처럼 보입니다. 동일하게 유지됩니다.반환 값은 변경되지 않습니다. - java

내가 잘못하고있는 부분에 대한 도움을받을 수 있습니까? 나는 간단한 총 클래스 프로그램을 만들고있다. 문제는 내 reload 메서드에서 발생합니다. 총에 총알을 다시로드하면 총의 bulletsRemaining이 최대 용량이되지만 탄환 값은 감소하지 않습니다.

public class Gun { 

    String name; 
    String sound; 
    int minDamage; 
    int maxDamage; 
    int shotsPerSecond; 
    int capacity; 
    int bulletsRemaining; 

    public Gun(String name, String sound, int minDamage, int maxDamage, int shotsPerSecond, int capacity, int bulletsRemaining) { 
     this.name = name; 
     this.sound = sound; 
     this.minDamage = minDamage; 
     this.maxDamage = maxDamage; 
     this.shotsPerSecond = shotsPerSecond; 
     this.capacity = capacity; 
     this.bulletsRemaining = bulletsRemaining; 
    } 

    public void fire() { 
     Random rnd = new Random(); 

     int totalDamage = 0; 
     for(int x = 0; x<shotsPerSecond; x++) 
     { 
      int damage = rnd.nextInt(maxDamage) + 1; 
      System.out.print(sound + "(" + damage + ")"); 
      totalDamage += damage; 
     } 

     System.out.println(" --> " + totalDamage + " Dmg/Sec "); 
    } 

    public void fireAtWill() { 
     Random rnd = new Random(); 

     int totalDamage = 0; 
     while(bulletsRemaining > 0) { 
     for(int x = 0; x<shotsPerSecond; x++) 
      { 
       int damage = rnd.nextInt(maxDamage) + 1; 
       System.out.print(sound + "(" + damage + ")"); 
       totalDamage += damage; 
       bulletsRemaining--; 
       if(bulletsRemaining == 0) 
        break; 
      } 

     System.out.println(" --> " + totalDamage + " Dmg/Sec "); 
     totalDamage = 0; 
     } 
    } 

    public int reload(int ammo) { 
     //System.out.println(); 
     //System.out.println("Bullets remaining: " + bulletsRemaining); 
     //System.out.println("Ammo remaining: " + ammo); 
     //System.out.println("Reloading " + name); 

     int bulletsReload = capacity - bulletsRemaining; //Amount of bullets to be loaded to gun 
     ammo = ammo -= bulletsReload; 
     bulletsRemaining = bulletsRemaining += bulletsReload; //Fill bulletsRemaining to the max again after reloading 
     return ammo; 
    } 

    public void supressiveFire(int ammo) { 

     Random rnd = new Random(); 

     int totalDamage = 0; 
     while(ammo != 0) { 
     while(bulletsRemaining > 0) { 
     for(int x = 0; x<shotsPerSecond; x++) 
      { 
       int damage = rnd.nextInt(maxDamage) + 1; 
       System.out.print(sound + "(" + damage + ")"); 
       totalDamage += damage; 
       bulletsRemaining--;     
      } 

      System.out.println(" --> " + totalDamage + " Dmg/Sec "); 
      totalDamage = 0; 
      if(bulletsRemaining == 0) 
        reload(ammo); 
     } 
     } 
     if(ammo == 0) 
      System.out.println("Out of ammunition)"); 

    } 

    public static void main(String[] args) { 
     // TODO code application logic here 

     int ammo = 5; 
     Gun g1 = new Gun("MK16", "Bang!", 10,15,5,5,5); 

     g1.fire(); 
     g1.reload(ammo); 
     System.out.println("Ammo left: " + ammo); 
     System.out.println("Bullets left: " + g1.bulletsRemaining); 


    } 

} 

예상 출력 :

Ammo: 0 
Bullets Remaining: 5 

출력 내가 접수 :

Ammo: 5 
Bullets Remaining: 5 
+0

는 당신이 우리를 말씀해 주시겠습니까? – Simon

+0

어느 부분에 대해? – Aloysius

+0

예. 그래서 내가하려는 것은 총을 만드는 것입니다. 그래서 총알이 0 개 남았을 때 재 장전 방법을 호출하면 지정한 탄약 값을 줄이면서 자동으로 최대 용량으로 다시로드합니다. – Aloysius

답변

2

당신은 아래처럼 다시 할당해야합니다. 에서

ammo = g1.reload(ammo); 

주 당신이 그것으로 무슨 일을하는지 조작

public static void main(String[] args) { 
     // TODO code application logic here 

     int ammo = 5; 
     Gun g1 = new Gun("MK16", "Bang!", 10,15,5,5,5); 

     g1.fire(); 
     ammo = g1.reload(ammo); 
     System.out.println("Ammo left: " + ammo); 
     System.out.println("Bullets left: " + g1.bulletsRemaining); 


    } 
+0

어디에서해야합니까? – Aloysius

+0

@Aloysius는 기본 방법입니다. – Beniton

+0

출력은 여전히 ​​동일하므로 탄환은 변경되지 않습니다. @Beniton – Aloysius