0
$\begingroup$

I have 2 arrays (s_left and s_right) a length (dis), the arrays in reality are produced from sensor values.

The first situation entails looping the condition (s_left < dis) twice and at the same time looping (s_right < dis) once.

Is there a method to specify the number of times a individual condition can be looped through whilst preforming various conditions? The coding is in Java and I am not convinced the method attempted below is correct

public class test {
    public void test() {
        double s_right[] = new double[]{10, 13, 14, 15};
        double s_left[] = new double[]{12.6, 14, 18, 18};

        double dis = 12.5;

        for (int i = 0; i < s_left.length; i++) {
            if (s_left[i] < dis) {
                for (int j = 0; j < s_left.length; j++) {
                    if ((s_left[j] < dis) && (s_right[i] < dis)) {
                        System.out.println("working!!!");
                    }

                }
            }
        }
    }
}
$\endgroup$
8
  • $\begingroup$ what are you trying to do? $\endgroup$ – jsotola Mar 18 '20 at 17:44
  • $\begingroup$ make the first comparison if ((s_left[i] < dis) && (s_right[i] < dis)) { ..... make the second comparison if (s_left[j] < dis) { $\endgroup$ – jsotola Mar 18 '20 at 17:49
  • $\begingroup$ So, I am trying to preform.... if (s_left[j] < dis) ....twice and at the same time preform the loop.... if (s_right[i] < dis).... twice, upon looping and verifying the loop statement is shall print ....System.out.println("working!!!");.....Hope this makes sence? $\endgroup$ – Neamus Mar 18 '20 at 18:32
  • $\begingroup$ This was my first attempt, I looped the (s_left[j] < dis) once then within the condition looped again but I think this will not work. the purpose is to verify the (s_left[j] < dis) twice before making the robot commence on its path. Same idea with the right side. $\endgroup$ – Neamus Mar 18 '20 at 20:23
  • $\begingroup$ @jsotola would you have any idea how to run the loop twice before making the robot move forward? $\endgroup$ – Neamus Mar 18 '20 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.