2017-10-29 83 views
-1

这是我的[R 我想找到我们在交换机大小写格式 代码排列组合的代码没有显示奥比语法错误 但一些代码行不执行

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() 

} 
+0

你能展示仍然存在问题的最小可重复代码,而不是倾销所有代码吗?不执行是非常含糊的。我想你需要'cat'或'print'那个'sprintf'部分。 – Axeman

+1

好的,使用例如'cat(sprintf(“C(n,r):%.10f”,comb),'\ n')',或者只是'cat('C(n,r):',comb,'\ n') '。 – Axeman

回答

0

在你的函数

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

返回NA

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

尝试使用:

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

注:看什么发生在你的功能上,你可以输入调试(梳子) https://stat.ethz.ch/R-manual/R-devel/library/base/html/debug.html 这样你就可以测试你的函数环境中发生了什么。我希望这能解决你的问题。