我想为毕达哥拉斯定理编写 R 代码。
毕达哥拉斯定理指出,斜边(直角的对边)的平方等于其他两条边的平方和。
(边A)^2+(边B)^2=斜边^2
现在我编写了如下的 R 代码:
pythag<-function(sidea,sideb){
if (sidea>=0&sideb>=0)
hypoteneuse=sqrt(sidea^2+sideb^2)
else if (sidea<0|sideb<0)
hypoteneuse<-"Values Need to be Positive"
else if (!is.vector(x))
hypoteneuse<-"I need numeric values to make this work"
print(hypoteneuse)
}
pythag(4,5)
pythag("A","B")
pythag(-4,-5)
在 pythag(4,5) 的情况下没关系,pythag(-4,-5) 也给出评论“值需要为正”。
但是在 pythag("A","B") 的情况下,我想评论“我需要数值来完成这项工作”,但不幸的是我的代码不适用于此。