76540

while loop not running until meeting completion condition

Question:

I have created a while loop which can't seem to run properly. it 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.

Answer1:

"The while loop of the useExample class should only stop when each condition of the while is false."

Using "&&" in while(a != a1 && b != b1 && c != c1 && d != d1 && e != e1){...} means that if only one condition is false e.g. (a != a1)==false, the whole condition will be false.

if you want the loop to run until all conditions are false, you should use "||" instead of "&&".

Recommend

  • Split Single Column into multiple and Load it to a Table or a View
  • Android WebView enters unrecoverable state if executed JavaScript enters infinite loop
  • golang debugger stops on empty lines - why?
  • UDPClient Receive method not working in a service
  • Restrict mouse movement over a specified window handle
  • Wpf RichTextBox wrapping problems
  • Linear gradient not applying in Webkit with d3 generated SVG
  • Waiting for $.post to finish
  • How read between delimiters in php DOM of a XML file?
  • How to change default stop edit behavior in jtable
  • What is this strange character in chrome's resource css viewer?
  • Creating NSCollectionView with datasource programatically
  • SQL: Getting the physical size of a subset of a table
  • Time out Error in send mail
  • Add dynamic data to line chart from mysql database with highcharts
  • Using Sax parsing to edit and write XML in VB6
  • java inputstream
  • Django: Count of Group Elements
  • Can I display google adwords (AdView) in javafx on android
  • Sending data from AppleScript to FileMaker records
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • sending/ receiving email in Java
  • php design question - will a Helper help here?
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?