某些代码行未执行

时间:2017-10-29 08:00:53

标签: r

这是我的R代码 我想在切换案例格式中找到我们的排列和组合 代码没有显示aby语法错误 但是

perm <- Fact(n) / Fact(n-r)
sprintf("P(n,r): %.10f", perm)

类似地

comb <- Fact(t) / (Fact(k)*Fact(t-k))
sprintf("C(n,r): %.10f", comb)

这两个函数中的这些语句都没有执行:

ProbAnl <- function()
{
#menu
print("Select choice: ")
print("1. Permutation")
print("2. Combination")

choice = as.integer(readline(prompt="Enter choice: "))

#switch case for menu
result <- switch(choice, Perm(), Comb())
}

Fact = function(num)
{
factorial <- 1  
for(i in 1:num) 
{
    temp = factorial * i
    factorial <- temp
    }
return(factorial)
}

Perm = function()
{
cat("Enter the required parameters: \n")

n <- as.integer(readline(prompt="Set size(n): "))
r <- as.integer(readline(prompt="No. of objects chosen from the set(r): "))

perm <- Fact(n) / Fact(n-r)
sprintf("P(n,r): %.10f", perm)  

repeat
{
    ans <- readline(prompt="Do you want to go back to the Probability 
                   Analysis Menu ?(y/n)\n")

    if(ans == 'y' | ans == 'n')
        break
    else
        cat("Wrong Input. Enter again.\n")

}

if(ans == 'y')
    ProbAnl()
else
    Perm()
}

Comb = function()
{
cat("Enter the required parameters: \n")

t <- as.integer(readline(prompt="Set size(n): "))
k <- as.integer(readline(prompt="No. of objects chosen from the set(r): "))

comb <- Fact(t) / (Fact(k)*Fact(t-k))
sprintf("C(n,r): %.10f", comb)

    repeat
{
    ans <- readline(prompt="Do you want to go back to the Probability Analysis Menu ?(y/n)\n")

    if(ans == 'y' | ans == 'n')
        break
    else
        cat("Wrong Input. Enter again.\n")
}

if(ans == 'y')
    ProbAnl()
else
    Comb()

}

1 个答案:

答案 0 :(得分:0)

在你的功能中

as.integer(readline(prompt="Set size(n): "))

返回NA

因为as.integer(“1”)返回NA

尝试使用:

as.integer(as.numeric(readline(prompt="Set size(n): "))

注意:要查看函数中发生的情况,可以键入 debug(Comb) https://stat.ethz.ch/R-manual/R-devel/library/base/html/debug.html  这样你就可以测试函数环境中发生的事情。我希望这能解决你的问题。

相关问题