差异问题,我最近写了一个小的模拟时遇到困难就找到了差异,我的等级如下表所示:的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得到方差作为过去的任务完成,所以我不知道我要去哪里错了这里,并希望另一双眼睛可能会导致一些答案。谢谢你的时间!
为什么您认为方差是错误的?它出什么问题了? – pjs