将 Ruby 翻译为 Java:Java 是否有类似于 Ruby switch 语句的构造
我正在尝试将 Ruby 中的代码转换为 Java。
到目前为止,我还无法在 Java 中遇到 switch 语句类型的构造,它可以让我在这些情况下调用方法。
有没有人对执行类似于下面的代码的替换构造有任何建议。
def move_validator
message = nil
case
when out_of_bounds?(@x_dest, @y_dest) == true
message = "You cannot move off the board"
when no_checker_at_origin? == true
message = "There is no checker to move in requested location"
when trying_to_move_opponents_checker? == true
message = "You cannot move an opponents checker"
when trying_to_move_more_than_one_space_and_not_jumping? == true
message = "You cannot move more than one space if not jumping"
when attempted_non_diagonal_move? == true
message = "You can only move a checker diagonally"
when attempted_move_to_occupied_square? == true
message = "You cannot move to an occupied square"
when non_king_moving_backwards? == true
message = "A non-king checker cannot move backwards"
when attempted_jump_of_empty_space? == true
message = "You cannot jump an empty space"
when attempted_jump_of_own_checker? == true
message = "You cannot jump a checker of your own color"
when jump_available_and_not_taken? == true
message = "You must jump if a jump is available"
else
move
if jumping_move?
message = "jumping move"
remove_jumped_checker
end
king_checkers_if_necessary
end
message
end
谢谢。
I'm trying to translate what the code I have, in Ruby, to Java.
So far I have been unable to come across a switch statement-type construct in Java that will let me call methods in the cases.
Does anyone have any suggestions for a replacement construct that performs similar to the code below.
def move_validator
message = nil
case
when out_of_bounds?(@x_dest, @y_dest) == true
message = "You cannot move off the board"
when no_checker_at_origin? == true
message = "There is no checker to move in requested location"
when trying_to_move_opponents_checker? == true
message = "You cannot move an opponents checker"
when trying_to_move_more_than_one_space_and_not_jumping? == true
message = "You cannot move more than one space if not jumping"
when attempted_non_diagonal_move? == true
message = "You can only move a checker diagonally"
when attempted_move_to_occupied_square? == true
message = "You cannot move to an occupied square"
when non_king_moving_backwards? == true
message = "A non-king checker cannot move backwards"
when attempted_jump_of_empty_space? == true
message = "You cannot jump an empty space"
when attempted_jump_of_own_checker? == true
message = "You cannot jump a checker of your own color"
when jump_available_and_not_taken? == true
message = "You must jump if a jump is available"
else
move
if jumping_move?
message = "jumping move"
remove_jumped_checker
end
king_checkers_if_necessary
end
message
end
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
缺点是您必须手动优化它们的运行顺序。
Updated:
Downside is you will have to manually optimize the order in which they go.
只是:
或者使用 test() 和 getMessage() 使每个条件成为您自己的 Condition 类的对象,并拥有一个 List。问题是 x、y 参数。
Just:
Or make every condition an object of your own Condition class with a test() and a getMessage() and have a List. Problem are the x, y parameters.