2016-03-04 53 views
-1

差異問題,我最近寫了一個小的模擬時遇到困難就找到了差異,我的等級如下表所示:的Java:在模擬

class aQuarter { 
    public int ordered; 
    public int leftOver; 
    public int profit; 
    public int profit2; 
    public double variance; 
    public int demand; 
    public int demandTotal; 
    public int counter; 
    public int sold; 
    public int averageProfit; 
    public double standardDev; 
    public int soldTotal; 
    public int orderedTotal; 



    public int getCounter() { 
     return counter; 
    } 
    public void incrementCounter() { //increments the counter for every week 
     counter++; 
    } 
    public int getDemand() { 
     return demand; 
    } 
    public void setDemand(int demand) { //sets the demand and adds that demand to the total demand for stat keeping 
     this.demand = demand; 
     demandTotal+=demand; 
    } 
    public int getDemandAverage() { 
     return demandTotal/counter; //returns the average demand 
    } 
    public int getProfitAverage() { 
     return profit/counter;  //returns the average profit 
    } 

    public int getOrderedAverage() { //returns how many ordered on average for that quarter 
     return orderedTotal/counter; 
    } 
    public void setOrdered(int ordered) { //sets how much was ordered 
     this.ordered = ordered; 
     orderedTotal+=ordered; 
    } 
    public int getleftOver() { //testing purposes 
     return leftOver; 
    } 
    public void setleftOver() { //sets how many gallons we have left over to deduct from the profit made 
     int x=ordered-demand; 
     if (x<0) {    //if the demand was more than we ordered, none leftover 
      leftOver=0; 
     } 
     else 
      leftOver=x;   //if we ordered more than was demanded, leftover = ordered-demand 

    } 
    public int getSoldAverage() { 
     return soldTotal/counter; //returns how many was sold average 
    } 
    public void setSold() { 
     int x = ordered-demand; 
     if (x<=0) {    //sets how many we sold 
      sold=ordered;  //if the demand was more than we ordered, we sold how much was demanded 
      soldTotal+=sold; //add that total to the soldTotal accumulator 
     } 
     else { 
      sold=demand;  //else, we ordered more than was demanded, making us sell how much was demanded 
      soldTotal+=sold; //add that total to the soldTotal accumulator 
     } 

    } 
    public int getProfit() { 
     return profit; 
    } 
    public void setProfit() {    //sets the profit made for the week 
     int baseProfit=sold*10 - leftOver*7; 
     profit+= sold*10 - leftOver*7;  //For every gallon we sold, we made 10 dollars,but for every gallon that 
     profit2+=(baseProfit*baseProfit); //was leftover, we lost 7 dollars 
    }          //add the profit squared to profit2 

    public double getVariance() {   //gets the variance of the simulation for the quarter 
     averageProfit=profit/counter; 
     variance=profit2/counter -averageProfit*averageProfit; 
     return variance; 
    } 
    public double getStandardDev() {  //gets the standard deviation for the quarter 
     standardDev=Math.sqrt(variance); 
     return standardDev; 
    } 

} 

而這是在「供」的該類別的通話時間循環。

  firstQuarter.incrementCounter(); //add 1 to the counter 
      firstQuarter.setOrdered(50+gallonsDelivered()); //set how many gallons ordered for quarter+ randomness 
      firstQuarter.setDemand(gallonsleftOver1()); //sets demand using the demands for the first quarter 
      firstQuarter.setSold(); //sets sold 
      firstQuarter.setleftOver(); //sets how many leftover to deduct from the profit made 
      firstQuarter.setProfit(); //sets the profit 

除了使我脫離這個世界的數字的變化之外,一切都按預期打印出來。

For Quarter 1: 
Average Profit Per Week: 444 
Average Demand Per Week: 54 
Average Ordered Per Week: 51 
Average Sold Per Week:47 
Average Variance of Profit: 8915.0 
Standard Deviation: 94.41927769264072 

我跟着precedure得到方差作爲過去的任務完成,所以我不知道我要去哪裏錯了這裏,並希望另一雙眼睛可能會導致一些答案。謝謝你的時間!

+0

爲什麼您認爲方差是錯誤的?它出什麼問題了? – pjs

回答

0

您在計算方差時使用的所有變量都是int變量!這是問題!正因爲如此,公式中的劃分不能按預期發揮作用。分配給double變量並不意味着發生的計算將是浮點計算。計算以整數計算進行,然後隱式轉換爲double

+0

我改變了班級中的所有變量加倍,並且仍然存在相同的問題 – Bob

+0

真的嗎?那麼你在這裏給出的場景的正確價值是什麼? – Pubudu

+0

可能在20年左右 – Bob