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?
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 + ", ");
}
}
}
}
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!