减少一个变量,直到在java中达到一个数字

减少一个变量,直到在java中达到一个数字,java,Java,我有这门课: public class Vehicle { private float speed; public void decelerationSpeed() { --speed; } } 每次减速速度方法都被称为速度变量,减少一个 我需要更改减速速度方法,这样,如果速度变量达到零并且调用了减速速度方法,则不必更改速度值 在本教程中,我不能使用if-else或任何其他条件运算符(我想我必须使用模运算和除法运算)。使用javas

我有这门课:

public class Vehicle {
    private float speed;

     public void decelerationSpeed()
     {
         --speed;
     }  
}
每次
减速速度
方法都被称为速度变量,减少一个

我需要更改
减速速度
方法,这样,如果速度变量达到零并且调用了
减速速度
方法,则不必更改速度值


在本教程中,我不能使用if-else或任何其他条件运算符(我想我必须使用模运算和除法运算)。

使用javas浮点到整数转换的好解决方案如下所示:

speed -= (int) ((speed / (speed + 1.0) + 0.6));

基本思想是java通过丢弃小数点将浮点数转换为整数。因此,我们只需要一种生成值
v
的方法,即
0我不知道是否允许位转换,但这里有一个解决方案:

speed -= 1;

int bits = Float.floatToRawIntBits(speed);
bits &= (bits >>> 31) - 1;
speed = Float.intBitsToFloat(bits);
因此,首先我们得到符号位(
bits>>31
)并从中减去一得到掩码:
0xffffffff
表示正数,
0x00000000
表示负数。使用此选项会将每个负数更改为
+0


这不是一个很好的解决方案,但它是有效的。

我们总是希望减去一,除非我们的速度为零,所以模运算是合适的,因为
0模y
0
,而对于任何其他数字,我们希望
x模y
得到
1
。符合这些标准的模运算是
x%(x-1)
两个角情况是1和2,其中1给出的模为0,
2模1
没有效果。因此,我们通过初步加法和后续减法将其从可能的值集中排除:

   public void decelerationSpeed()
     {
        speed = speed + 2;
        speed = speed - ((speed) % (speed-1));
        speed = speed - 2;
     }

如果可以使用
Math.sqrt
函数,则可以使用以下等式

(val+|val|)/2
基本上,任何负数都会从自身减去0

这里有一些代码显示它

public static float decelerationSpeed(float speed) {
    speed -= 1; // subtract
    // use (val+|val|)/2 to zero out any negative numbers. Divide by 2 to return to origional number
    return (float) (speed+Math.sqrt(speed*speed))/2f;
}

public static void main(String [] args) throws Exception {
    System.out.println(assertF(decelerationSpeed(-10), 0));
    System.out.println(assertF(decelerationSpeed(10), 9));

    System.out.println(assertF(decelerationSpeed(-2), 0));
    System.out.println(assertF(decelerationSpeed(2), 1));

    System.out.println(assertF(decelerationSpeed(-1), 0));
    System.out.println(assertF(decelerationSpeed(1), 0));

    System.out.println(assertF(decelerationSpeed(-0), 0));
    System.out.println(assertF(decelerationSpeed(0), 0));
}

public static float assertF(float actual, float expected) {
    if(actual != expected) throw new IllegalStateException("Expected "+expected+" but got "+actual);
    return actual;
}

我会这样回答这个问题。通过int、short、float或double等的工程:

public class HelloWorld
{
  private static float speed = 1113; // initial speed
  private static short smallIncrement = 13;

  public static void main(String[] args)
  {
      decelerateAndReturnSpeed(2.33); // prints 1110.67
      decelerateAndReturnSpeed(7.33f); // prints 1103.3401
      decelerateAndReturnSpeed(113); // prints 990.3401
      decelerateAndReturnSpeed(smallIncrement); // prints 977.3401
  }

  public static float decelerateAndReturnSpeed(double vAmt)
  {
    speed -= vAmt;
    if (speed < 0) speed = 0;
    System.out.println(speed);
    return speed;
  }
}

我现在不能测试它,所以我不会把它作为一个答案来添加,但这可能会起作用:
speed+=(speed%1)-1
复制的副本有几种“max”的方式,但这也可以很容易地调整为“min”的形式。每次减少后只需检查最小值:
——speed;如果(速度<0f)速度=0f
@Tom他说他不能使用if-else语句
Float。floatToIntBits
在其实现中使用
if
。不要
Float.floatorawintbits
Float.intBitsToFloat
也这样做吗?@omid我不认为他们这样做,但我可能错了:如果速度可以是0到1之间的某个值,这不起作用。@Bubletan sry,完全忽略了那个约束。@Paul我不认为这是一个约束。速度值相对于整个整数步长是离散的。所以,即使速度是一个浮点值,也永远不会以0到1之间的某个值开始或结束。这是假设加速度也发生在int步中。@Anatoly,我想这是一个有点奇怪的问题。OP的澄清在这里会很有帮助。。。
public class HelloWorld
{
  private static float speed = 1113; // initial speed
  private static short smallIncrement = 13;

  public static void main(String[] args)
  {
      decelerateAndReturnSpeed(2.33); // prints 1110.67
      decelerateAndReturnSpeed(7.33f); // prints 1103.3401
      decelerateAndReturnSpeed(113); // prints 990.3401
      decelerateAndReturnSpeed(smallIncrement); // prints 977.3401
  }

  public static float decelerateAndReturnSpeed(double vAmt)
  {
    speed -= vAmt;
    if (speed < 0) speed = 0;
    System.out.println(speed);
    return speed;
  }
}
speed = (float)(speed -vAmt);  // same as speed =- vAmt