0

the program calls a method from another class which helps change the stop condition for my program. It calls the method about 3 - 8 times on average and never reaches the stopping condition, however it stops.

    public class useExample
    {
     public static void main(String[] args)
     {

       Example ex = new Example();
       long [] result;

       long a = 0;
       long b = 0;
       long c = 0;
       long d = 0;
       long e = 0;
       int count = 0;

       int a1 = 1;
       int b1 = 2;
       int c1 = 3;
       int d1 = 4;
       int e1 = 5;


      for(int i = 0; i <1; i++)
      {
        while(a != a1 && b != b1 && c != c1 && d != d1 && e != e1)
        {
          result = ex.getOnes();
          a = result[0];
          b = result[1];
          c = result[2];
          d = result[3];
          e = result[4];

          System.out.println(result[0] + " " + result[1] + " " + result[2] + " " + result[3] + " " + result[4]);

        System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + count++);

        }
       System.out.println(a + " " + b + " " + c + " " + d + " " + e + " "+ count);

      }



     }
   }

The Example class is a follows:

import java.util.*;

public class Example 
{
  Random r = new Random();

    public long[] getOnes(){

        int a = r.nextInt(35);
        int b = r.nextInt(35);
        int c = r.nextInt(35);
        int d = r.nextInt(35);
        int e = r.nextInt(35);

        while(a == 0)
        {
            a = r.nextInt(35);
            //temp[0] = a;
        }

        while(b == 0 || b == a /*|| b == c || b == d || b == e*/)
        {
            b = r.nextInt(35);
            //temp[1] = b;
        }

        while(c == 0 || c == a || c == b /*|| c == d || c == e*/)
        {
            c = r.nextInt(35);
            //temp[2] = c;
        }

        while(d == 0 || d == a || d == b || d == c/*|| d == e*/)
        {
            d = r.nextInt(35);
            //temp[3] = d;
        }

        while(e == 0 || e == a || e == b || e == c|| e == d)
        {
            e = r.nextInt(35);
            //temp[4] = e;
        }

        return new long[] {a, b, c, d, e};
    }

}

The while loop of the useExample class should only stop when each condtion of the while is false.This means when:

a == a1
b == b1 
c == c1 
d == d1 
e == e1

It should output how many while loops it went through and the values of each loop. eventually outputting the same values of a1 to e1.

3
  • By the way: a much easier way to pick 5 random numbers from 1 to 34 is to put the numbers 1-34 into a list, use Collections.shuffle, and then just take the first (or last) 5 numbers. Commented Jun 14, 2017 at 20:36
  • Also: your end condition would be much more easily expressed using Arrays.equals to compare the arrays, rather than comparing the individual elements. Commented Jun 14, 2017 at 20:43
  • Why did you ask that question twice? Commented Jun 14, 2017 at 21:39

2 Answers 2

1

The while loop of the useExample class should only stop when each condtion of the while is false.This means when:

a == a1
b == b1 
c == c1 
d == d1 
e == e1

Then your while cycle is wrong now and should look like:

while (a != a1 || b != b1 || c != c1 || d != d1 || e != e1)

Because the condition that while contains means not when to stop but when to continue

Sign up to request clarification or add additional context in comments.

Comments

0

The while loop ends as soon as one of the conditions is met. If you want all the conditions to be met, please use || instead of &&.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.