作业中的结构错位
我首先在 Eclipse 中完成所有编程作业,然后将它们放入 Putty 中并提交给我们的老师。在 Eclipse 中,我的方法之一出现了这个奇怪的错误。
它显示“标记上的语法错误,错误的构造。”
public static int factorial(int iVal, boolean DEBUG)
{
int result;
// Defensive programming
if(iVal <= 0)
{
System.out.println("Error: iVal cannot be a negative 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
它的错误位于由
System.out.println(" Currently working on " + i);
“有什么想法吗?”组成的行上。
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 negative 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
比较是
==
,赋值是=
。另外,如果您只是测试布尔值,则根本不需要进行比较,只需使用
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 语句中有一个赋值:
if(DEBUG = true){
这是合法的(并且可以编译),因为
DEBUG
的类型是boolean
,但它始终是真实。
You have an assignment in an if statement:
if(DEBUG = true){
This is legal (and compiles) because
DEBUG
is of typeboolean
, but it is alwaystrue
.