Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: Java  |  size: 0.86 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. public class Question1 {
  3.         public static void main(String[] args) {
  4.  
  5.                 int checkPrime = 2;
  6.  
  7.                 //fixed loops because STYLE IS EVERYTHING DARLING
  8.                 for (int y = 0; y < 10; y++) {
  9.  
  10.                         int x = 0;
  11.                         while(x < 10) {
  12.                                 if(printPrime(checkPrime))
  13.                                         x++;
  14.                                 checkPrime++;
  15.                         }
  16.                        
  17.                         System.out.println();
  18.                 }
  19.  
  20.         }
  21.  
  22.         private static boolean printPrime(int checkPrime) {
  23.                
  24.                 //if you want to escape from a loop like that, its better
  25.                 // to leave the loop with return or break than use another
  26.                 // variable. It also makes it much more efficient because
  27.                 // the loop exits as soon as it knows its not a prime
  28.                 for (int i = 2; i <= checkPrime; i++) {
  29.                         if (checkPrime % i == 0 && checkPrime != i) {
  30.                                 return false;
  31.                         } else if (i == checkPrime) {
  32.                                 System.out.print(checkPrime + "\t");
  33.                                 return true;
  34.                         }
  35.  
  36.                 }
  37.                
  38.                 return false;
  39.         }
  40. }