作业中的结构错位

发布于 2024-09-30 04:34:56 字数 933 浏览 1 评论 0原文

我首先在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-10-07 04:34:57
if(DEBUG = true)

比较是==,赋值是=
另外,如果您只是测试布尔值,则根本不需要进行比较,只需使用

if(DEBUG)
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)
飘逸的'云 2024-10-07 04:34:57

您在 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 type boolean, but it is always true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文