我似乎无法弄清楚这个“世界必须被声明为最终”的错误。有什么建议吗?我正在制作一个网格世界项目(配对游戏)。该段
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;
}
});
}
}
最佳答案
由于您尝试在方法中的局部类内使用局部变量,因此必须将该变量声明为 final
。因此,只需将 final
修饰符添加到变量声明中即可:
final World world = new World();
与 main
方法本地的其他变量类似,但不适用于本地类,例如临时数组
。
关于java - 如何使数组成为最终数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184658/