V
V
Vladimir Abramov2021-11-17 18:56:04
Java
Vladimir Abramov, 2021-11-17 18:56:04

Comparing value with array values ​​and finding closest value?

Good afternoon, can someone guide me on the right path? I can't complete the task. There is a value, you need to compare it with an array and display the nearest value. Increment step in array 0.25

btn_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

float num1 = Float.parseFloat(sp_dens.getSelectedItem().toString());
float num2 = Float. parseFloat(sp_dens_sec.getSelectedItem().toString());
float total = num1 - num2;

/* for (int i = 0; i < array_alcohol.length; i++) {

if (total <= (Float.parseFloat(array_alcohol[ i]) - 0.25) || total >= (Float.parseFloat(array_alcohol[i]) + 0.25))
{
tv_result.setText(String.valueOf(total));
}
}*/

tv_result.setText(String.valueOf(total));
}
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question