Home > database >  If statement being skipped, even though the values match
If statement being skipped, even though the values match

Time:10-23

Why is my if statement here being skipped? Directly to else:

override fun onSensorChanged(event: SensorEvent) {
        runOnUiThread {
            val xAxis = event.values[0]
            val yAxis = event.values[1]
            val zAxis = event.values[2]
            Log.d("Eixos", /*"Eixo X: $xAxis, Eixo Y: $yAxis,*/ "Eixo Z: $zAxis")
            if (zAxis in 0.900000..-0.900000){
                imagemCelular!!.setImageResource(R.drawable.phoneright)
                Log.d("Phonin", "Certo")
            }else{
                imagemCelular!!.setImageResource(R.drawable.phonewrong)
                Log.d("Phonin", "Errado")
            }
        }
    }

Here is a pic of the compiler, the value is indeed between what I need, but it's like my if statement is not even there.

PrintScreen

CodePudding user response:

By defining the range from large to small, Kotlin will assume this range is empty. If you flip the range around so it goes smallest to largest, this should work:

Change:

if (zAxis in 0.900000..-0.900000)

To:

if (zAxis in -0.900000..0.900000)

Or:

if (zAxis in -0.9..0.9){

CodePudding user response:

I think you must change position between 0.9 and -0.9 because operator function define is

override fun contains(value: T): Boolean = lessThanOrEquals(start, value) && lessThanOrEquals(value, endInclusive)
  • Related