
Untitled
By: a guest on
Apr 16th, 2012 | syntax:
Java | size: 0.86 KB | hits: 19 | expires: Never
public class Question1 {
public static void main(String[] args) {
int checkPrime = 2;
//fixed loops because STYLE IS EVERYTHING DARLING
for (int y = 0; y < 10; y++) {
int x = 0;
while(x < 10) {
if(printPrime(checkPrime))
x++;
checkPrime++;
}
System.out.println();
}
}
private static boolean printPrime(int checkPrime) {
//if you want to escape from a loop like that, its better
// to leave the loop with return or break than use another
// variable. It also makes it much more efficient because
// the loop exits as soon as it knows its not a prime
for (int i = 2; i <= checkPrime; i++) {
if (checkPrime % i == 0 && checkPrime != i) {
return false;
} else if (i == checkPrime) {
System.out.print(checkPrime + "\t");
return true;
}
}
return false;
}
}