如何创建增量功能 [英] How to create increment funtion

查看:68
本文介绍了如何创建增量功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我得到了我需要的重新运行a应该在循环内但是当我写的那个显示bug。



我尝试过:



float moveValue(float b = 0.0f)

{



浮动a = 0.0f;



for(float i = 0.0001f; i< 5; i ++)

{



a = i;





}





返回a;

if i get what i need the rerun a should be inside of the loop But when i wrote that showed bug .

What I have tried:

float moveValue( float b = 0.0f)
{

float a =0.0f;

for (float i = 0.0001f; i < 5; i++)
{

a =i;


}


return a;

推荐答案

看起来像你'我假设增量将是你开始使用的相同值,而事实并非如此。正如格里夫已经指出的那样,增量总是1(在你的情况下为0r 1.0)。



如果你想使用除整数增量以外的任何东西,你有使用 do / while 循环。此外,由于您正在使用浮点值,因此如果您希望循环以5.0精确停止,则应调用 Math.Round



It looks like you're assuming that the increment will be the same value you're starting our with, and that's not the case. As Griff already pointed out, the increment will always be 1 (0r 1.0 in your case).

If you want to use anything other than an integer increment, you have to use a do/while loop. Also, since you're working with a floating point value, you should call Math.Round if you want the loop to stop at precisely 5.0.

double i = 0d;
do 
{
    i = Math.Round(i+0.00001d, 5);

    // do something with the new value

} while (i < 5.0d);





如果这不是您想要做的,请改进您的问题,以便有人可以为您提供你需要的答案。



If this isn't what you're trying to do, improve your question so someone can provide you with the answer you need.


这篇关于如何创建增量功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆