I'm not real sure what this is SUPPOSED to do, but try this:
public class perfectnums
{
public static void main(String[] args)
{
int t = 2;
int c = 0;
for (int n=1; n < 5; n++ )
{
t++;
c = 2^(t-1)*(2^t-1);
if (c > 0)
{
System.out.println(c);
}
}
}
}
Notice the removal of the extra variables, the change in the test for c being GREATER than ZERO and the change of the "while" to a "for"
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Actually what was the problem with the original code was this:
if (a < 0)<strong>;</strong>
{
}
Semicolon are used to terminate commands. So when you put that semicolon, you terminated the if statement, and rest was printed anyway.
This code is acceptable:
{
// commands;
}
{
// other commands;
}
Meaning that you can open and close brackets without having to add 'if' or 'while'.
So the ';' terminated the if (); and rest code was outside the if and it was executed anyway.
What you wrote:
if (a < 0);
{
}
Was like writing this:
if (a<0) {}
System.out.println(a);
Or this:
if (a<0) {}
{System.out.println(a);}
When in fact you needed this:
if (a<0) {
System.out.println(a);
}
or
if (a<0)
System.out.println(a);
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
When is a going to be less than zero?
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Well does it compile?
Do you get any errors?
What is the problem?
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448