java - How to save prime number (1-100) in array and call the 2 prime randomly -


how save prime number in code array , call 2 random of them

this edited code#3 suggested @stephen c

public class prima {  public  static int[] getprima(int aw, int ak) {         int [] primes;         int jmlprim=0;          int h = 0;         primes = new int[5];          boolean isprima;         (int = 2; <= ak; i++) {             isprima = true;             (int j = 2; j < i; j++) {                 if (i % j == 0) {                     isprima = false;                     break;                 }             }             if (isprima == true) {                 //system.out.print(i + " ");                 jmlprim++;                 primes[jmlprim]=i;              }          }             /*here code random prime number*/             final random random = new random();         final int i1 = random.nextint(primes[jmlprim]);         final int prime1 = primes[i1];         system.out.println( "i1= " + i1);          return null;      } public static void main(string[] args) {        system.out.println(prima.getprima(1, 10));  }  } 

i want save result of prima.getprima(1,100) in array,

before understanding @stephen c mean, edit code above,

@stephen c, please correct that..., how check or ensure prime number stored in

primes[jmlprim]; 

for addition, add code random value primes[jmlprim];

final random random = new random();             final int i1 = random.nextint(primes[jmlprim]);             final int prime1 = primes[i1];             system.out.println( "i1= " + i1); 

and when run code

array= 2,array= 3,array= 5,array= 7,i1= 4 null 

why i1=4?? know 4 isn't prime number

given nature of question, "a learning exercise" ... i'm not going provide code.

here's "first cut" "save primes in array" part:

  1. change method's return type int[].
  2. allocate array of appropriate size hold primes.
  3. add variable next position in array add prime.
  4. when find prime, add this:

     primes[index] = prime;  index++; 
  5. return array

this basic "java 101" stuff. if have difficulty understanding how it, review lecture notes, text book or oracle java tutorial.

(note doesn't deal issue don't know how many primes there between awal , akhir.)


the second part of question ... "i have no idea how call 2 random of them" ... incomprehensible. i'm not going guess might mean.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -