java - Adding a new object to an array according to loop returns -
i did research on subject (i'm quite new java excuse me poor/incorrect wording) , didn't find related answers, so, here goes.
i'm working on program can print prime numbers , ran following problem.
here's code:
public class pnumba { public static void main(string[] args) { int pnlimit = 100; system.out.println("printing prime numbers"); (int numer = 2; numer <= pnlimit; numer++) { if (checkforprime(numer)) { system.out.println(numer); } } } public static boolean checkforprime(int numer) { (int = 2; < numer; i++) { if (numer % == 0) { return false; } } return true; } }
my program prints prime numbers up 100 , not 100 of them. idea make array hold 100 prime numbers , break loop when said array reaches limit fix problem.
my questions following:
- how make loop fill array when
checkforprime()
method returns true? - do have use
break;
stop loop when reaches 100 prime numbers? - can use
<=arrayname.length
stopfor
loop alternative?
please tell me if need more info.
change main block follow:
int counter = 1; system.out.println("printing prime numbers"); (int numer = 2; counter <= 100; numer++){ if (checkforprime(numer)) { system.out.println(numer); counter++; } }
here counter execute loop until 100 prime numbers displayed...............!
Comments
Post a Comment