r - 重复 else if 语句的快捷方式

标签 r if-statement

如果条件如下,我必须重复编写 else:

  if (d_hand[1]==1){
    state=score(p_hand)-1
  } else if (d_hand[1]==2){
    state=19+score(p_hand)
  } else if (d_hand[1]==3){
    state=39+score(p_hand)
  } else if (d_hand[1]==4){
    state=59+score(p_hand)
  } else if (d_hand[1]==5){
    state=79+score(p_hand)
  } else if (d_hand[1]==6){
    state=99+score(p_hand)
  }

你知道它是否可以写得更高效/更短吗? 我想过做一个 if 循环,但效率会降低,因为必须检查每个语句。

最佳答案

根本没有ifelse:

state <- score(p_hand) + 20 * d_hand[1] - 21

关于r - 重复 else if 语句的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49676353/

相关文章:

r - 图标签 : add text on graphs in the same location despite figure size

swift - 值为 Int 但 case 可以包含数组的 Switch 语句

ios - 如何结束 if-else 循环而不导致程序崩溃?

r - 错误 : Unhandled Exceptions when trying to deploy/(re)publish ShinyApps.

r - 设置多边形的背景颜色

r - 绘制多列的分布

Python:如何测试 IF 语句对 Dictionary Case 语句的效率?

mysql - 简单的 MySQL if 查询语法

vb.net - 我的 For 循环跳过了 IF 语句

r - 更改传单中传单笔画的线宽