本文介绍了重构重复后卫声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我定义,计算获胜的几率,并返回所许给玩家数量的方法。我原本写了这个完成此任务的最明显的方式,但我真的想知道这个重构,以尽量减少计算机在执行的工作量的最佳方式。这里是我的功能:
INT getPay(INT winningOdds,诠释派息)
{
winningOdds =兰特()%MAXIMUM_ODDS + MINIMUM_ODDS; 如果(winningOdds == 1)
{
支出= 4000;
的printf(!大奖 - %d个\\ N,派息);
}
否则如果(winningOdds&下; = 2)
{
支出= 1000;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则如果(winningOdds&下; = 3)
{
支出= 500;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则,如果(winningOdds< = 40)
{
支出= 100;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则,如果(winningOdds< = 50)
{
支出= 50;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则,如果(winningOdds< = 75)
{
支出= 25;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则如果(winningOdds&下; = 1000)
{
支出= 10;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则,如果(winningOdds< = 3000)
{
支出= 5;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则,如果(winningOdds< = 5000)
{
支出= 3;
的printf(赢得的硬币内容 - %d \\ n,派息);
}
否则,如果(winningOdds< = 10000)
{
支出= 1;
的printf(赢得的硬币内容 - %d \\ n,派息);
} 返回支付;
}
解决方案
尝试这种风格,你需要填写的阵列中的奖金
结构赔率{
INT winningOdds,派息;
};INT getPay(INT winningOdds,诠释派息)
{
赔付支出[] = {{1,4000},{} 2,1000 ....};
INT numElem = sizeof的(支出)/ sizeof的(派息[0]); winningOdds =兰特()%MAXIMUM_ODDS + MINIMUM_ODDS;
的for(int i = 0; I< numElem;我++){
如果(winningOdds< =派息[I] .winningOdds){
支出=支出[I] .payout;
打破;
}
}
的printf(赢得的硬币内容 - %d \\ n,派息);}
I've defined a method that calculates the odds of winning and returns the amount payed to the player. I originally had written this the most obvious way of accomplishing this task, but I really would like to know the best way of refactoring this to minimize the amount of work the computer is performing. Here's my function:
int getPay(int winningOdds, int payOut)
{
winningOdds = rand() % MAXIMUM_ODDS + MINIMUM_ODDS;
if(winningOdds == 1)
{
payOut = 4000;
printf("Jackpot! - %d\n", payOut);
}
else if(winningOdds <= 2)
{
payOut = 1000;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 3)
{
payOut = 500;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 40)
{
payOut = 100;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 50)
{
payOut = 50;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 75)
{
payOut = 25;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 1000)
{
payOut = 10;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 3000)
{
payOut = 5;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 5000)
{
payOut = 3;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 10000)
{
payOut = 1;
printf("Coins won - %d\n", payOut);
}
return payOut;
}
解决方案
Try this style, you need to fill in the array of Payout
struct Payout{
int winningOdds, payOut;
};
int getPay(int winningOdds, int payOut)
{
Payout payout[] = {{1,4000},{2,1000}....};
int numElem = sizeof(payout)/sizeof(payout[0]);
winningOdds = rand() % MAXIMUM_ODDS + MINIMUM_ODDS;
for(int i=0;i<numElem ;i++){
if(winningOdds <=payout[i].winningOdds){
payOut = payout[i].payout;
break;
}
}
printf("Coins won - %d\n", payOut);
}
这篇关于重构重复后卫声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!