Page 1 of 1

Install problem or run time problem?

Posted: Tue Sep 02, 2003 1:56 pm
by laosland
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

Posted: Tue Sep 02, 2003 2:08 pm
by MudGuard

Code: Select all

 for (a=1;a>300;a++)
{
this loop is never entered: a is set to 1, the loop will run as long as a is bigger than 300.
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...

Posted: Tue Sep 02, 2003 2:23 pm
by laosland
:oops: 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++;
}


}

}

}
/***************************************************

Posted: Tue Sep 02, 2003 3:46 pm
by s_reynisson

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++;
	}
}
}
}

Posted: Tue Sep 02, 2003 4:02 pm
by laosland
:D I really, really appreciate the help. Now I really feel like a dunce, dividing by 1..doh!!!
Thanks for your help guys.
Andy