为什么我的所有数组索引值都设置为相同的值

时间:2015-07-19 18:39:04

标签: java

我程序的这部分似乎给了我一个问题:

public static double[] getBonusAmt(boolean[] bonusEligibility, int[] numYrsFlown, double[] bonusAmt) {  
      bonusAmt = new double[bonusEligibility.length];  
      double bonus = 0;  
      for (boolean b : bonusEligibility) {    
         for (int i : numYrsFlown) {       
            if (i >= 9 && b == true) {
               bonus = 2410.00;
            }
            else if (i < 9 && i >= 6 && b == true) {
               bonus = 1206.00;
            }
            else if (i < 6 && i >= 2 && b == true) {
               bonus = 515.00;
            }
            else if (i < 2 && b == true) {
               bonus = 0.00;
            }                        
         } 
      }      
      return bonusAmt; 
   }

输入/输出:

Name: [joe, james]
Years flown: [2, 2]
Miles flown: [45, 43]
Average miles between pilots: 44
Bonus eligibility: [true, false]
Bonus amount: [0.00, 0.00]

Joe应该获得奖金,因为他的飞行里程大于平均值,但他的数量为零。 Joe的预期奖金金额应该是515.00,因为一个,他有资格获得奖金,两个,只飞行了2年。

任何人都可以看到为什么奖金金额总是为零,即使我进入另一个飞行超过平均水平的人?

3 个答案:

答案 0 :(得分:3)

您的方法会为bonus变量分配值,但会返回一个永不分配的bonusAmt变量,因此其值仍为0.0

你的嵌套循环没有多大意义。看起来你需要一个常规的for循环,假设bonusEligibility数组的第i个索引与numYrsFlown数组的第i个索引相对应。

public static double[] getBonusAmt(boolean[] bonusEligibility, int[]  numYrsFlown) {  
    double[] bonusAmt = new double[bonusEligibility.length];    
    for (int i = 0; i < bonusEligibility.length; i++) {           
        if (numYrsFlown[i] >= 9 && bonusEligibility[i]) {
           bonus = 2410.00;
        }
        else if (numYrsFlown[i] < 9 && numYrsFlown[i] >= 6 && bonusEligibility[i]) {
           bonusAmt[i] = 1206.00;
        }
        else if (numYrsFlown[i] < 6 && numYrsFlown[i] >= 2 && bonusEligibility[i]) {
           bonusAmt[i] = 515.00;
        }
        else if (numYrsFlown[i] < 2 && bonusEligibility[i]) {
           bonusAmt[i] = 0.00;
        }                         
    }      
    return bonusAmt; 
}
顺便说一句,将bonusAmt数组作为参数传递给方法是没有意义的,因为该方法为其分配了对新数组的引用。

答案 1 :(得分:0)

您忘记将bonusAmt设置为所选的bonus值。

答案 2 :(得分:0)

这是一种更面向对象的方式来做你想要的。无需接受这个答案,因为Eran的解决方案完美地解释了您的错误......这只是另一种方式......

public class MainApp {

  public static void main(String[] args) {
    AirMilesCustomer[] customers = new AirMilesCustomer[] {
        new AirMilesCustomer("John", true, 2),
        new AirMilesCustomer("Jane", true, 5),
        new AirMilesCustomer("Sally", true, 7),
        new AirMilesCustomer("Bill", false, 10),
        new AirMilesCustomer("Stacy", true, 15)
    };

    for(AirMilesCustomer customer : customers) {
      System.out.println(customer);
    }
  }
}

class AirMilesCustomer {
  private String _name;
  private boolean _bonusEligibility;
  private int _numYrsFlown;

  public AirMilesCustomer(String name, boolean bonusEligibility, int numYrsFlown) {
    _name = name;
    _bonusEligibility = bonusEligibility;
    _numYrsFlown = numYrsFlown;
  }

  public String getName() {
    return _name;
  }

  public boolean isBonusEligibility() {
    return _bonusEligibility;
  }

  public int getNumYrsFlown() {
    return _numYrsFlown;
  }

  public double getBonusAmount() {
    double bonus = 0.00;

    if (_numYrsFlown >= 9 && _bonusEligibility) {
      bonus = 2410.00;
    }
    else if (_numYrsFlown < 9 && _numYrsFlown >= 6 && _bonusEligibility) {
      bonus = 1206.00;
    }
    else if (_numYrsFlown < 6 && _numYrsFlown >= 2 && _bonusEligibility) {
      bonus = 515.00;
    }
    else if (_numYrsFlown < 2 && _bonusEligibility) {
      bonus = 0.00;
    }
    return bonus;
  }

  public String toString() {
    return "[" + _name + "][" + _numYrsFlown + "][" + _bonusEligibility + "][" + getBonusAmount() + "]";
  }
}