-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