gpt4 book ai didi

java - 减少和增加 float 增量android

转载 作者:行者123 更新时间:2023-12-01 10:16:48 25 4
gpt4 key购买 nike

我正在尝试通过屏幕按钮增大和减小音量。我这里有代码:

//variables
float leftVolume = 0.5f;
float rightVolume = 0.5f;

public void lowerVolume(View view)
{ float decrement = 0.2f;
rightVolume -= decrement;
leftVolume -= decrement;
Log.d("Values","This is the value of volume"+ leftVolume);
}

public void raiseVolume(View view)
{ float increment = 0.2f;
rightVolume = ++increment;
leftVolume = ++increment;
Log.d("Values","This is the value of volume"+ rightVolume);
}

日志显示了一些疯狂的值,例如当我单击 rasieVolume 时,它​​会变为 1.2f 并保持在那里。

最佳答案

发生这种情况是因为您每次调用 raiseVolume() 时都将浮点值设置为 1.2 。

public void raiseVolume(View view)
{ float increment = 0.2f; //you set increment here
rightVolume = ++increment; //then simply increase it by 1
leftVolume = ++increment;

//...and so your volume will always be 1.2 at this point
Log.d("Values","This is the value of volume"+ rightVolume);
}

解决这个问题的方法是将音量设置为raiseVolume()之外的初始值。方法(您已经这样做了),然后在 raiseVolume() 内增加它方法。

像这样:

//variables
float leftVolume = 0.5f;
float rightVolume = 0.5f;

public void raiseVolume(View view)
{ float increment = 0.2f;
rightVolume += increment; //change this line to this
leftVolume += increment; //change this line to this
Log.d("Values","This is the value of volume"+ rightVolume);
}

关于java - 减少和增加 float 增量android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com