Misplaced construct in homework
I do all my programming assignments in eclipse first before putting them in putty and submitting them to our teacher. In eclipse, I have this strange error in one of my methods.
it says "Syntax error on token(s), misplaced construct(s)."
public static int factorial(int iVal, boolean DEBUG)
{
int result;
// Defensive programming
if(iVal <= 0)
{
System.out.println("Error: iVal cannot be a negat开发者_如何学JAVAive number!");
System.exit(0);
}
// Calculate result
int factor = iVal;
int counter = iVal - 1;
for(int i = counter; i > 1; i--)
{
if(DEBUG = true)
{
System.out.println("DEBUG");
System.out.println(" Finding the factorial of " + factor);
System.out.println(" Currently working on " + i);
System.out.println(" With an intermediate result of" + iVal);
}
iVal *= i;
}
result = iVal;
// Return result
return result;
} // End of factorial method
It has the error placed on the line consisting of
System.out.println(" Currently working on " + i);
Any ideas?
if(DEBUG = true)
Comparison is ==
, assignment is =
.
Also, if you are just testing a boolean value, you don't need to do a comparison at all and just use
if(DEBUG)
You have an assignment in an if statement:
if(DEBUG = true){
This is legal (and compiles) because DEBUG
is of type boolean
, but it is always true
.
精彩评论