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
Install problem or run time problem?
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
this loop is never entered: a is set to 1, the loop will run as long as a is bigger than 300.Code: Select all
for (a=1;a>300;a++) {
As 1 is NOT bigger than 300 it will not run at all.
same for the loop with b.
Therefore (as all your outputs are within that for (a=... loop which will not be used, nothing is written to output.
So: neither install nor runtime problem - faulty programming...
Thanks...
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;
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++;
}
}
}
}
/***************************************************
- s_reynisson
- Posts: 939
- Joined: Tue May 06, 2003 1:59 pm
Code: Select all
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++;
}
}
}
}