Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Scala类型不匹配,Can';我不能解决这个问题_Java_String_Scala_Mismatch - Fatal编程技术网

Java Scala类型不匹配,Can';我不能解决这个问题

Java Scala类型不匹配,Can';我不能解决这个问题,java,string,scala,mismatch,Java,String,Scala,Mismatch,所以,对于一个项目,我试图制作一个游戏生成的方块移动的特定部分。所有坐标都存储在一个列表中,通过“x”和“y”值,我应该能够将坐标相加,从而使块移动 def movement(move: Point): Unit = { val newList: List[Point] = placed val xx = move.x; val yy = move.y for (i <- newList.indices) newList(i) += Point(xx, yy)

所以,对于一个项目,我试图制作一个游戏生成的方块移动的特定部分。所有坐标都存储在一个列表中,通过“x”和“y”值,我应该能够将坐标相加,从而使块移动

  def movement(move: Point): Unit = {
    val newList: List[Point] = placed
    val xx = move.x; val yy = move.y
    for (i <- newList.indices) newList(i) += Point(xx, yy)
  }
def移动(移动:点):单位={
val newList:列表[点]=已放置
val xx=move.x;val yy=move.y
对于(i列-1)0其他x
y=if(y<0)nrRows-1 else if(y>nrRows-1)0 else y
点(x,y)
}
def moveAnimal():单位={
如果(!gameOver){
def newAnimalFront:点={
val newHead:Point=currentDir匹配{
case East()=>playAnimal.head+点(1,0)
case West()=>playAnimal.head+点(-1,0)
case North()=>playAnimal.head+点(0,-1)
case South()=>playAnimal.head+点(0,1)
}
checkForWrap(新头)
}
playAnimal=newAnimalFront+:playAnimal.init
}
}

但是,此方法在我当前的项目中显示字符串不匹配。

您需要做两件事:

  • 在类中定义
    方法
    +
  • 避免突变(这取决于您,但您的代码会变得更可读)
  • 然后你可以这样写smth:

      def main(args: Array[String]): Unit = {
        val placed: List[Point] = List(Point(0, 0), Point(1, 1))
        println(placed.mkString) // Point(0,0)Point(1,1)
        val moved = movement(Point(2, 2), placed)
        println(moved.mkString) //Point(2,2)Point(3,3)
      }
      def movement(move: Point, placed: List[Point]): List[Point] = {
        // here you create new list and don't mutate the old one
        placed.map(p => p + move)
      }
      case class Point(x: Int, y: Int) {
        def +(p: Point) = Point(x + p.x, y + p.y)
      }
    

    如果
    newList(i)
    是一个
    ,那么
    点+=点
    应该做什么?@jwvh例如,如果块必须向下移动,程序显示移动(点(0,1)),则新点应添加到新列表(i)中。我可能做得不正确。您没有将其添加到列表中,而是将其添加到列表中的索引
    I
    ,这是
    点的一个实例。1-您想用新元素附加
    newList
    ,还是想调整
    newList
    中的每个元素?2-Scala
    列表
    是不可变的,由于
    movement()
    方法返回
    Unit
    (即nothing),它对任何东西都没有影响。如果
    Point
    具有
    +
    方法(即
    Point+Point
    是可能的),这并不意味着
    +=/code>是可能的。使用
    .map()
    创建新的调整后的
    列表[点]
    ,然后将其分配给
    var
    变量。(顺便说一句,这段代码的Scala风格很差。)这解决了我的问题!非常感谢你。
      def main(args: Array[String]): Unit = {
        val placed: List[Point] = List(Point(0, 0), Point(1, 1))
        println(placed.mkString) // Point(0,0)Point(1,1)
        val moved = movement(Point(2, 2), placed)
        println(moved.mkString) //Point(2,2)Point(3,3)
      }
      def movement(move: Point, placed: List[Point]): List[Point] = {
        // here you create new list and don't mutate the old one
        placed.map(p => p + move)
      }
      case class Point(x: Int, y: Int) {
        def +(p: Point) = Point(x + p.x, y + p.y)
      }