2016-07-06 5 views
1

私のパラメータ値は、渡した後に変更されず、編集されて返されません。戻り値は変更されません - java

私が間違って行っている箇所を助けてもらえますか?私は単純な銃のクラスプログラムを作成しています。問題は私のリロードメソッドで発生します。弾に弾をリロードすると、銃の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

ammo = g1.reload(ammo); 

あなたはそれをやっていることな操作を教えてもらえますか? – Simon

+0

どの部分ですか? – Aloysius

+0

だから、私がやろうとしているのは銃を作ることです。弾丸が残った後にリロードメソッドを呼び出すと、指定された弾薬の値を減らしながら自動的に最大の容量にリロードします。 – Aloysius

答えて

2

あなたは以下のようにそれをバック割り当てる必要があります。メイン

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

関連する問題