gpt4 book ai didi

java - 为什么这个方法不执行这段代码中的其他方法?

转载 作者:行者123 更新时间:2023-12-02 03:59:40 25 4
gpt4 key购买 nike

我在多个场景中多次运行代码并得到了相同的结果。它应该做的是向右移动,当它到达它应该转弯的点时,它就会转弯。相反,它会向我显示错误对话框,我指示它在遇到无法识别的内容时显示该错误对话框。它似乎跳过了告诉它执行单独方法的代码部分。我还有其他三种类似的方法,它们在不同的方向上做同样的事情。

详细说明情况如何。我开始游戏,它会加载玩家所在的关卡。我使用某种形式的输入来执行下面的代码。角色向右移动,但随后拒绝移动,即使关卡规定应该移动。

编辑:我修改了代码以使用 .equals() 而不是 ==。我发现问题实际上是由于我忘记了java如何处理x和y轴而出现的。我感谢那些修复了我的编码缺陷的人,这确实很有帮助。

public static void moveRight(){
move = true;
while(move == true){
Point character = load.getCharacter();
Point space = character;
space.x += 20;
String name = load.getSpace(space);
System.out.println(name);
if(name.equals("empty")){
load.setCharacter(space);
}else if(name.equals("fTurnUp")){
load.setCharacter(space);
move = false;
moveUp();
}else if(name.equals("fTurnDown")){
load.setCharacter(space);
move = false;
moveDown();
}else if(name.equals("fTurnLeft")){
load.setCharacter(space);
move = false;
moveLeft();
}else if(name.equals("fTurnRight")){
load.setCharacter(space);
move = false;
moveRight();
}else if(name.equals("turn")){
load.setCharacter(space);
move = false;
}else if(name.equals("finish")){
load.setCharacter(space);
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"Congratulations! You have completed the level.",
"Level Completed",
JOptionPane.PLAIN_MESSAGE);
}else if(name.equals("wall")){
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"The level was not designed correctly. Click 'Ok' to return to main menu",
"Invalid Level",
JOptionPane.ERROR_MESSAGE);
}
}
}

最佳答案

尝试将比较器从 == 更改为 .equals()。前者指向字符串的引用,而后者实际上比较

鉴于此,应该是:

if(name.equals("empty")){
load.setCharacter(space);
}else if(name.equals("fTurnUp")){
load.setCharacter(space);
move = false;
moveUp();
}else if(name.equals("fTurnDown")){
load.setCharacter(space);
move = false;
moveDown();
}else if(name.equals("fTurnLeft")){
load.setCharacter(space);
move = false;
moveLeft();
}else if(name.equals("fTurnRight")){
load.setCharacter(space);
move = false;
moveRight();
}else if(name.equals("turn")){
load.setCharacter(space);
move = false;
}else if(name.equals("finish")){
load.setCharacter(space);
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"Congratulations! You have completed the level.",
"Level Completed",
JOptionPane.PLAIN_MESSAGE);
}else if(name.equals("wall")){
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"The level was not designed correctly. Click 'Ok' to return to main menu",
"Invalid Level",
JOptionPane.ERROR_MESSAGE);
}

关于java - 为什么这个方法不执行这段代码中的其他方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com