Java 发现不兼容类型:void,怎么了?

Java 发现不兼容类型:void,怎么了?,java,types,vector,void,Java,Types,Vector,Void,我试图编写一个类,找到最接近的两个向量并返回一个和 我已经尽力去理解,但是我找不到我收到这条信息的原因,这是我唯一的错误: java:93:不兼容类型 发现:无效 必需:EDU.gatech.cc.is.util.Vec2 结果=1.2; ^ 第93行在代码的末尾,我放了一些箭头来表示它 enter code here package EDU.gatech.cc.is.clay; import java.util.*; import EDU.gatech.cc.is.clay.*; im

我试图编写一个类,找到最接近的两个向量并返回一个和

我已经尽力去理解,但是我找不到我收到这条信息的原因,这是我唯一的错误:

java:93:不兼容类型 发现:无效 必需:EDU.gatech.cc.is.util.Vec2 结果=1.2; ^

第93行在代码的末尾,我放了一些箭头来表示它

enter code here


package EDU.gatech.cc.is.clay;


import java.util.*;
import EDU.gatech.cc.is.clay.*;
import java.lang.*;
import EDU.gatech.cc.is.abstractrobot.*;
import EDU.gatech.cc.is.util.Vec2;
import EDU.gatech.cc.is.util.Units;


public class MAX_go_in_between extends NodeVec2
{
  public static final boolean DEBUG = /*true;*/ Node.DEBUG;
  private SocSmall abstract_robot;


  public MAX_go_in_between(SocSmall ar)
  {
    abstract_robot = ar;


  }

  long last_spott = 0;
  Vec2 result = new Vec2();



  public Vec2 Value(long timestamp)
  {


    if (DEBUG) System.out.println("MAX_Avoid_walls: Value()");

    if ((timestamp > last_spott) || (timestamp == -1))
    {
      if (timestamp != -1) last_spott = timestamp;


      Vec2 one;
      Vec2 two;

      //array of Vec2 of all the opponents
      Vec2[] list_opp = abstract_robot.getOpponents(timestamp);
      //empty array of vec2 where will be put the opponents in front of the robot
      ArrayList<Vec2> list_opp_in_front;

      Vec2 temp;


      // find which opponents are in front and put them in the arraylist
      for(int i=0; i<list_opp.length; i++)
      {
        temp = list_opp[i];

        if(temp.x >= 0.0)
        {
          list_opp_in_front.add(temp);
        }
      }

      //get closest opponent and sets it to index 0
      for(int i=1; i<list_opp_in_front.size()-1; i++)
      {
        temp = list_opp_in_front.get(i);

          if(list_opp_in_front.get(0).r<temp.r)
        {
          list_opp_in_front.set(i, list_opp_in_front.get(0));
          list_opp_in_front.set(0, temp);

        }
      }

      //get second closest opponent and sets it to index 1
      for(int i=2; i<list_opp_in_front.size()-1; i++)
      {
        temp = list_opp_in_front.get(i);

          if(list_opp_in_front.get(1).r<temp.r)
        {
          list_opp_in_front.set(i, list_opp_in_front.get(1));
          list_opp_in_front.set(1, temp);
        }

          // sum both vectors
          one = list_opp_in_front.get(0);
          two = list_opp_in_front.get(1);

 =============>>>>
 =============>>>>   result = one.add(two);
          }

      }

      return(result);
    }

  }



Here is the Vec2.add(Vec2) method:


 public void add(Vec2 other)
  {
  x = x + other.x;
  y = y + other.y;
  r = Math.sqrt(x*x + y*y);
  if (r > 0)
   t = Math.atan2(y,x);
  }
由此,成员函数add不会返回任何您可以输入到结果中的内容。用这样的线条:

x = x + other.x;
如果x是当前对象的一个成员,而另一个是您要添加到其中的对象,那么一个是绝对确定的。添加两个意味着修改一个,而不仅仅是在计算中使用它

因此,不是:

one = list_opp_in_front.get (0);
two = list_opp_in_front.get (1);
result = one.add (two);
您可能需要以下内容:

result = list_opp_in_front.get (0);
two = list_opp_in_front.get (1);
result.add (two);
由此,成员函数add不会返回任何您可以输入到结果中的内容。用这样的线条:

x = x + other.x;
如果x是当前对象的一个成员,而另一个是您要添加到其中的对象,那么一个是绝对确定的。添加两个意味着修改一个,而不仅仅是在计算中使用它

因此,不是:

one = list_opp_in_front.get (0);
two = list_opp_in_front.get (1);
result = one.add (two);
您可能需要以下内容:

result = list_opp_in_front.get (0);
two = list_opp_in_front.get (1);
result.add (two);

根据您的方法声明public void addVec2 other,您正在将两个添加到一个中。因此,一本身就是你的结果,因此不需要回报


只需删除return语句,并将其中一个作为结果对象。

根据您的方法声明public void addVec2 other,您正在将两个添加到一个中。因此,一本身就是你的结果,因此不需要回报


只需删除return语句,并将其中一个语句作为result对象。

指出第93行如何。另外,方法Vec2.addVec2的声明是什么?请分享Vec2的add方法声明。指出哪一行是第93行如何。另外,方法Vec2.addVec2的声明是什么?请分享来自Vec2的add方法声明。好的,我已经用箭头指示了第93行并添加了Vec2.addVec2,谢谢您的快速回答!我还不习惯在这里提问,对不起!好的,我已经用箭头指示了第93行并添加了Vec2.addVec2,谢谢您的快速回答!我还不习惯在这里提问,对不起@负面用户:请留下一些评论来帮助理解答案有什么问题?@负面用户:请留下一些评论来帮助理解答案有什么问题?