将 Ruby 翻译为 Java:Java 是否有类似于 Ruby switch 语句的构造

发布于 2024-12-13 06:10:16 字数 1478 浏览 3 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2024-12-20 06:10:16

更新:

if(condition)  
{  
   function();  
}  
else if( condition)  
{  
   function();  
}  

缺点是您必须手动优化它们的运行顺序。

Updated:

if(condition)  
{  
   function();  
}  
else if( condition)  
{  
   function();  
}  

Downside is you will have to manually optimize the order in which they go.

他不在意 2024-12-20 06:10:16

只是:

if (outOfBound(y, y)) {
    message = "...";
} else if (attemptedNonDiagonalMove()) {
    message = "...";
} else {
    ...
}

或者使用 test() 和 getMessage() 使每个条件成为您自己的 Condition 类的对象,并拥有一个 List。问题是 x、y 参数。

Just:

if (outOfBound(y, y)) {
    message = "...";
} else if (attemptedNonDiagonalMove()) {
    message = "...";
} else {
    ...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文