Outputting correct prime numbers in Java -
Outputting correct prime numbers in Java -
this question has reply here:
boolean , == vs = 3 answersi have little issue java programme doing @ moment:
public class primenumbers { public static void computeprimenumbers(boolean[] prime, int n) { (int = 2; < n; i++) { if (prime[i] == true) { (int j = 2; j < n; j++) { if ((j%i == 0) && (j != i)) { prime[j] = false; //system.out.println(j + " = " + prime[j]); } } } } } public static void main(string[] args) { int n = 13; boolean[] prime = new boolean[n]; prime[0] = false; prime[1] = false; (int = 2; < n; i++) { prime[i] = true; } computeprimenumbers(prime, n); (int = 0; < n; i++) { system.out.println(i + " = " + prime[i]); } } }
the programme should output "true" if integer (i) prime, , false if otherwise. commented out piece of code in if-statement within nested loop gives me right boolean statements (i.e. shows integers aren't prime numbers when running it), lastly loop within main seems show initial values given prime array - assume results computeprimenumbers beingness overwritten somehow, somewhere.
thanks help in advance.
the if
status uses assignment operator instead of equality operator:
if (prime[i] = true) { // should if(prime[i] == true) or if(prime[i])
prime[i] = true
assign value true
array element , if
status evaluate true.
java primes
Comments
Post a Comment