r - 第 2、3、4 和 7 行出现意外令牌错误
问题描述
enter code here
fn = function(a, b, c) {
if ((b^2 - 4ac) > 0) {
root1 = ((-b) + sqrt((b^2) - 4 a c)) / (2 * a)
root2 = ((-b) - sqrt((b^2) - 4 a c)) / (2 * a)
print(root1)
print(root2)
} else if (b*2 -4a*c == 0) {
root = -b / (2*a)
print(root)
} else {
print("There are no real roots.")
}
}
意外令牌“ac”第 2 行
意外标记“a”第 3 行
意外标记“a”第 4 行
意外标记“a”“*”第 7 行
解决方案
您需要*
产品,例如,
fn <- function(a, b, c) {
if ((b^2 - 4 * a * c) > 0) {
root1 <- ((-b) + sqrt((b^2) - 4 * a * c)) / (2 * a)
root2 <- ((-b) - sqrt((b^2) - 4 * a * c)) / (2 * a)
print(root1)
print(root2)
} else if (b * 2 - 4 * a * c == 0) {
root <- -b / (2 * a)
print(root)
} else {
print("There are no real roots.")
}
}
这样
> fn(1, -2, 1)
[1] "There are no real roots."
> fn(1,1,0)
[1] 0
[1] -1
推荐阅读
- python - Django - 创建超级用户
- scala - 通过创建 JSON 对象使用数据生成器的空手道 DSL 返回 null
- javascript - 创建表单输入的实时预览
- android-webview - 有没有办法缩放视频元素的分辨率以匹配来自媒体流的分辨率?
- java - Spring boot rabbitmq 配置繁重操作
- c++ - C++ 继承:具有基类类型的变量
- assembly - 更改为保护模式会导致三重故障
- python-3.x - __init__ 需要什么
- python - 如何使用 pandas 创建具有有限数量新列的数据透视表?
- python - Flask 视图接受 1 个位置参数,但给出了 2 个