有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在我的代码中找不到符号变量java error

我有下面的代码,它应该设置一个初始值为null的术语,然后通过for循环返回经过修改的术语。 然而,当我试图编译时,给出了一个错误,称为变量项找不到符号(在if语句/for循环末尾的第10行)。 我不明白为什么会出现这个错误,也不知道如何修复它。 任何帮助都将不胜感激

 public Term nextTerm()
   {
   double coefficient = 0.0;
   int exp = 0;
   Term term = new Term(coefficient,exp);
        for (exp = 0; exp < sequence.length ; exp++){
       double[] diffArray = differences();
       if (allEqual() == false) {
           coefficient = diffArray[0]/factorial(exp);
           term = Term(coefficient, exp);
        }
    }
   return term;
}

共 (2) 个答案

  1. # 1 楼答案

    试试这个:

    term = new Term(coefficient, exp);
    

    而不是

    term = Term(coefficient, exp);
    
  2. # 2 楼答案

    JLS 15.9. Class Instance Creation Expressions

    A class instance creation expression is used to create new objects that are instances of classes.

    • UnqualifiedClassInstanceCreationExpression: new [TypeArguments] ClassOrInterfaceTypeToInstantiate ( [ArgumentList] )[ClassBody]

    因此,将其改为以下内容:

    term = new Term(coefficient, exp);