我一直在编程约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