-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()
}
你能展示仍然存在问题的最小可重复代码,而不是倾销所有代码吗?不执行是非常含糊的。我想你需要'cat'或'print'那个'sprintf'部分。 – Axeman
好的,使用例如'cat(sprintf(“C(n,r):%.10f”,comb),'\ n')',或者只是'cat('C(n,r):',comb,'\ n') '。 – Axeman