本文介绍了rand()用于带有变量的c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int userHP = 100;
int enemyHP = rand() % ((userHP - 50) - (userHP - 75)) + 1;

好吧,由于某种原因,这似乎无法正常工作,我想获得50-25马力敌人。

okay, for some reason this doesnt seem to work right, im trying to get 50 -25 hp for enemys.

也是id,而是一个百分比... like

also id rather it be a percentage... like

int enemyHP = rand() % ((userHP / 50%) - (userHP / 75%)) + 1;

但是id喜欢坚持整数,而不是混乱浮动或双打...有人可以帮助我

but id like to stick with integers and not mess with floats or doubles... can someone help me?

推荐答案

对此执行一些代数:

rand() % ((userHP - 50) - (userHP - 75)) + 1;

rand() % (userHP - 50 - userHP + 75) + 1;

rand() % (userHP - userHP - 50 + 75) + 1;

rand() % (-50 + 75) + 1;

...可以快速看到出错了。为什么不使用双打?

...and you can quickly see what's going wrong. Why not use doubles?

这篇关于rand()用于带有变量的c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:39