samedi 28 mars 2015

Can you add an array list to a variable in java



I have the following code:



public static void main (String args[])
{

Scanner input = new Scanner(System.in);
System.out.println("\nLab1a\n");
final int MAX = 100;
boolean primes[];
primes = new boolean[MAX];


ArrayList<Integer>PrimeFactor = new ArrayList<Integer>();
for (int i = 1; i < MAX + 1; i++)
{
PrimeFactor.add(i);
}

System.out.println("Computing Prime Numbers");
System.out.println();
System.out.println("Primes Between 1 and 100");
System.out.println(PrimeFactor);
}

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
for (int i = 0; i < PrimeFactor.size(); i++)
{
if (isPrime(PrimeFactor.get(i)))
{ PrimeFactor.remove(i);}
}
}

public static boolean isPrime(int n)
{
for (int i = 2; i < n; i++)
{
if (n%i == 0)
return false;
}
return true;
}


}


So what I want to do is output the prime numbers from the numbers 1 to 100. The problem is whenever I output, I only get the numbers 1 to 100. How can I fix that? I see the problem because PrimeFactor is being used for two different things, I'm not sure. So please help.




Aucun commentaire:

Enregistrer un commentaire