Results 1 to 2 of 2
Thread: I need your help!!! Please?
- 11-23-2012, 08:45 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
I need your help!!! Please?
hi guys this is the code so far but i need the upperLimit to be input from the user please help me?
Java Code:public class Prime { private static int upperLimit = 1000; private static boolean[] flags; public Prime() { } public static void main(String[] args) { initialize(args); findPrimes(); displayPrimes(); } private static void initialize(String[] args) { if (args.length > 0) { upperLimit = Integer.parseInt(args[0]); } flags = new boolean[upperLimit + 1]; for (int position = 0; position <= upperLimit; position++) { flags[position] = false; } } private static void findPrimes() { for (int position = 2; position <= Math.sqrt(upperLimit); position++) { if (!flags[position]) { int multiple = position * 2; while (multiple <= upperLimit) { flags[multiple] = true; multiple += position; } } } } private static void displayPrimes() { for (int position = 2; position <= upperLimit; position++) { if (!flags[position]) { System.out.print(position + ", "); } } } }Last edited by JosAH; 11-23-2012 at 09:07 PM. Reason: added [code] ... [/code] tags
-
Re: I need your help!!! Please?
Read up on how to use a Scanner object (check out the tutorials that you can find on Google), and give that a try. You'll likely find that it will suit your needs well. Best of luck and welcome to the forum!


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks