如何制作阵列最终?

时间:2014-03-04 22:37:48

标签: java final gridworld

我似乎无法弄清楚这个“世界必须被宣布为最终”的错误。有什么建议?我正在制作一个gridworld项目(匹配游戏)。细分

  temparray.add(world.getLocation().getColor() );

要求它是最终的...但是添加最终的world.get等会说非法的表达开始。

import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import info.gridworld.world.*;



import java.awt.Color;
import java.util.ArrayList;

public class cardGameDriver extends ActorWorld
   {
   public static void main( String[] args )
  {
  World world = new World();
  world.show();

  world.add( new card1());
  world.add( new card1()); 
  world.add( new card2()); 
  world.add( new card2());
  world.add( new card3());
  world.add( new card3());
  world.add( new card4());
  world.add( new card4());
  world.add( new card5());
  world.add( new card5());
  world.add( new card6());
  world.add( new card6());
  world.add( new card7());
  world.add( new card7());
  world.add( new card8());
  world.add( new card8());
  world.add( new card9());
  world.add( new card9());
  world.add( new card10());
  world.add( new card10());
  world.add( new card11());
  world.add( new card11());
  world.add( new card12());
  world.add( new card12());
  world.add( new card13());
  world.add( new card13());
  world.add( new card14());
  world.add( new card14());
  world.add( new card15());
  world.add( new card15());
  world.add( new card16());
  world.add( new card16());
  world.add( new card17());
  world.add( new card17());
  world.add( new card18());
  world.add( new card18());
  System.out.println( "Time yourself to see how fast you can make all 18 matches." );


  ArrayList<cards> temparray = new ArrayList<cards>();


  java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager() 
  .addKeyEventDispatcher(new java.awt.KeyEventDispatcher() 
  {
  public boolean dispatchKeyEvent(java.awt.event.KeyEvent event) 
     { 
     String key = javax.swing.KeyStroke.getKeyStrokeForEvent(event).toString(); 
     int x = 0;
     if (key.equals("pressed ENTER")) 
        {
        temparray.add(world.getLocation().getColor() );
        x++;
        if( x == 2 )
           {
           if( temparray.get(0) == temparray.get(1) )
              {
              System.out.println( "User has made a match!" );
              }
           else
              {
              System.out.println( "Try again!" );
              }
           x = 0;
           }
        }
     return true; 
       } 
  }); 

  }

}

2 个答案:

答案 0 :(得分:5)

由于您尝试在方法中的本地类中使用局部变量,因此必须将该变量声明为final。因此,只需将final修饰符添加到变量声明:

final World world = new World();

类似于main方法本地的其他变量,而不是本地类的变量,例如temparray

答案 1 :(得分:0)

试试这个:final World world = new World();

“非法开始表达”可能与此无关 可能你只是没有以正确的方式标记final