I've just written a simple problem to find prime numbers and when I run it, it finished wihtout sending out any output other than the blank DOS shell telling me to Press any key to continue. Here's the code:
public class PrimeNumb
{
public static void main (String[] args)
{
int myPrime[] = new int [300];
int a=1,b=1,x=1, primeTrigger=1;
for (a=1;a>300;a++)
{
for (b=1;b>300;b++)
{
if (a%b>0)
{
primeTrigger=0;
}
}
if (primeTrigger==1)
{
System.out.println (a);
myPrime[x]=a;
x++;
}
}
}
}
/************************************************
Any ideas?
Thanks,
Andy
I guess I had a brain fart, as I was thinking that the second argument was *Do untill this is true*. Doh! Anyways, I rewrote it and the problem still happens:
/***************************************************
public class PrimeNumb
{
public static void main (String[] args)
{
int myPrime[] = new int [300];
int a=1,b=1,x=1;
boolean primeTrigger=true;
for (a=1;a<=300;a++)
{
for (b=1;b<=300;b++)
{
if (a%b>0)
{
primeTrigger=false;
break;
}
}
if (primeTrigger==true)
{
System.out.println (a);
myPrime[x]=a;
x++;
}
public class PrimeNumb
{
public static void main (String[] args)
{
int myPrime[] = new int [300];
int a=1,b=1,x=1;
for (a=1;a<=300;a++)
{
boolean primeTrigger=true; // reset for each number tested
for (b=2;b<=a-1;b++) // don't test for one and self
{
if (a%b==0) // is div and thus flunks as prime
{
primeTrigger=false;
break;
}
}
if (primeTrigger)
{
System.out.println (a);
myPrime[x]=a;
x++;
}
}
}
}