为什么我的数组存储了相同值的多个副本

时间:2015-07-17 16:20:40

标签: java

这是输出的样子:

Name: [a, b, c, d]
Years flown: [1, 11, 7, 2]
Bonus amount: [0.0, 2410.0, 0.0, 2410.0, 1206.0, 0.0, 2410.0, 1206.0, 515.0]
Average traveled miles: 666.66
Number earning bonus: 3
Total bonus amounts: $10157.00
Number of miles flown: [222.22, 555.55, 1000.0, 888.88]
Pilot count: 4

任何人都能看出错误吗?因为有4个名字,我只应该获得4个奖金,但我得到了所有这些额外的金额。此外,至于获得奖金的数量,应该只有2,因为你只能获得奖金是你的飞行里程数大于平均值,但它显示3人获得奖金。

public class PilotBonus {
   public static void main(String[] args) {        
      while (question.equalsIgnoreCase("n")) {
         if (pilotCount < MAX_PILOTS) {
            pilotCount++;       
            pilotName = getName(pilotName);
            numYrsFlown = getYrsFlown(numYrsFlown);
            numMilesFlown = getMilesFlown(numMilesFlown);
            avgMilesFlown = getAvg(pilotCount, numMilesFlown);
            bonusEligibility = determine(avgMilesFlown, numMilesFlown);
            bonusAmt = getBonusAmt(bonusEligibility, numYrsFlown, bonusAmt);
            numEarningBonus = getNumEarningBonus(bonusEligibility, numYrsFlown);
            totalBonus = calcTotalBonus(bonusAmt);
         }
         else {
            JOptionPane.showMessageDialog(null, "Too many pilots");
         }    
      }         
      if (question.equalsIgnoreCase("y")) {
         print(pilotName, numYrsFlown, bonusAmt, avgMilesFlown, numEarningBonus, 
            totalBonus, numMilesFlown, pilotCount); 
      } 
   }

   public static boolean determine(double avgMilesFlown, double[] numMilesFlown) {

      for (double d : numMilesFlown) {
         if (d > avgMilesFlown) {        
            return true;
         }          
      }
      return false;
   }

   public static double[] getBonusAmt(boolean bonusEligibility, int[] numYrsFlown, double[] bonusAmt) {

      double bonus = 0;

      if (bonusEligibility == true) {
         for (int i : numYrsFlown) {       
            if (i >= 9) {
               bonus = 2410.00;
            }
            if (i < 9) {
               bonus = 1206.00;
            }
            if (i < 6) {
               bonus = 515.00;
            }
            if (i < 2) {
               bonus = 0.00;
            }

            bonusAmt = Arrays.copyOf(bonusAmt, bonusAmt.length + 1);
            bonusAmt[bonusAmt.length - 1] = bonus;
         }        
      }

      return bonusAmt; 
   }

   public static int getNumEarningBonus(boolean bonusEligibility, int[] numYrsFlown) {

      int count = 0;

      for (int i : numYrsFlown) {
         if (i >= 2 && bonusEligibility == true) {
            count++;
         }
      }

      return count;
   }

1 个答案:

答案 0 :(得分:0)

您应该从double[] bonusAmt中删除getBonusAmt参数,并将同名变量添加到此方法的开头

double[] bonusAmt = new double[0];