2015-07-19 90 views
0

我的計劃的這部分似乎是給了我一個問題:爲什麼我所有的數組索引值設置爲相同的值

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] 

喬應該可以賺因爲他飛行的飛行裏數比平均值大,所以獎金是零。喬的預期獎金金額應該是515.00,因爲一個,他有資格獲得獎金和兩個,只能飛行2年。

任何人都可以看到爲什麼獎金總額爲零,即使我輸入的飛行次數超過了平均值嗎?

+0

你從未在'bonusAmt'的值。 –

回答

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陣列作爲參數傳遞給該方法是沒有意義的,因爲該方法分配給它一個新的數組的引用。

+0

啊,gotcha,謝謝! – john

0

您忘記將bonusAmt設置爲所選的bonus值。

0

這是一個更加面向對象的方式來做你想做的事情。如伊蘭的解決方案完美地解釋你的錯誤無需接受這個答案...這是做的只是另一種方式......

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() + "]"; 
    } 
} 
相關問題