【发布时间】:2013-08-15 06:47:36
【问题描述】:
它只是不断循环。数字继续减少,直到程序关闭。我是不是在滥用什么东西?
playerHealth 和 orcHealth 整数为 100。
randomNumber = ("%10d", 1 + (rand() % 100));
这就是我看到 srand() 解释页面上使用的随机数的方式。如果这是错的,应该怎么办?
这里还有其他问题吗?
switch(charDecision)
{
case 1:
cout << "FIGHT" << endl;
do{
randomNumber = ("%10d", 1 + (rand() % 100));
if(randomNumber >= 50){
orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
}
else
{
playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl;
}
}while(playerHealth || orcHealth >= 0);
break;
default:
break;
}
【问题讨论】:
-
嗯,
rand调用确实 有模数偏差。我建议使用<random>。
标签: c++ infinite-loop do-while