我一直在編程約3星期,我在做這個遊戲CIV。唯一的問題是在每輪比賽中,每輪比賽的統計數據都會更新,但第二輪之後他們不會更新。基本上我希望程序要做的是在每輪之後加入每個資源並計算人口和黃金,但這並不是在第一輪之後發生的。我從來沒有上過課,所以不要指望我第一次就做對。C++變量沒有更新在While循環
這裏是應該發生在函數內部每一輪的更新代碼:
int RoundTotal(int yg, int yk, int yf, int ys, int yr, int yfi,
int co, int rtp, int gtp, int ap, double tr, int yp, int dp,
int int yd, double fp) {
int YourGold = yg, YourStrength = ys, YourKnow = yk, YourFood = yf,
YourResource = yr, YourFields = yfi, YourPopulation = yp, YourDefense = yd;
int ResourceTradeProfit = rtp, GoldTradeProfit = gtp, DroughtProduction = dp;
int totals, count = co, ArcherPay = ap;
double taxrate = tr, FoodProduction = fp;
if (YourStrength<0) {
YourStrength = 0;
}
FoodProduction = (0.5*YourFields + 0.5*YourKnow - 0.02*YourPopulation)*DroughtProduction;
YourFood = YourFood + FoodProduction;
YourGold = YourGold + (taxrate/100)*YourPopulation;
YourGold -= (YourStrength/2);
YourGold -= YourKnow;
YourGold -= YourFood;
YourGold -= ArcherPay;
YourResource += ResourceTradeProfit;
YourGold += GoldTradeProfit;
YourPopulation = YourPopulation + YourFood*FoodProduction;
return totals, YourGold, YourKnow, YourFood, YourStrength,
YourResource, YourFields, count, ResourceTradeProfit,
GoldTradeProfit, ArcherPay, taxrate, YourPopulation,
DroughtProduction, FoodProduction;
不顧一切的縮寫爲變量向上頂,除非他們的問題。
你期望回報呢? – tkausl
你的函數只返回一個'int'所以對這個函數的調用方面,你只得到一個值。從設計角度來看,您應該創建一個包含所有這些數據的類,以便您可以簡單地返回該類或向該類添加更新方法或類似的東西。 – pstrjds
你或許應該閱讀有關[逗號操作符(http://en.cppreference.com/w/cpp/language/operator_other),這SO發佈[這裏](https://stackoverflow.com/q/54142/ 416574) – pstrjds