我在我的代码中使用 JOptionPane 并且我有这个块:
Object[] move = { "Up", "Down" };
Object moveValue = JOptionPane.showInputDialog(null,
"Where do you want to move?", "Input",
JOptionPane.INFORMATION_MESSAGE, null, move,
move[0]);
我多次使用上面的块,我在想,有没有办法给
Object[] move;
新的价值观。当我尝试时
Object[] move = { "Up", "Down", "Left" };
它说"Duplicate local variable move"
,当我尝试
move = { "Up", "Down", "Left" };
它给出了错误"Array constants can only be used in initializers"
。
那么有没有办法让我多次使用我的变量“移动”?
编辑:
代码的更大部分是
while (true) {
switch(hero.getPos()) {
case 1:
Object[] move = { "Up", "Down" };
Object moveValue = JOptionPane.showInputDialog(null,
"Where do you want to move?", "Input",
JOptionPane.INFORMATION_MESSAGE, null, move,
move[0]);
move = null;
switch ((String) moveValue) {
case "Üles":
hero.setPos(hero.getPos() + 1);
break;
case "Paremale":
hero.setPos(hero.getPos() + 5);
break;
}
break;
case 2:
System.out.println("Draakon!!!!");
dragon = new Dragon(1);
fight = new Fight(hero, dragon);
break;
case 3:
System.out.println("hallo!");
move = { "Up", "Right", "Left" };
所以我在第四行和最后一行都定义了“移动”。最后一个给出错误。