2017-10-29 82 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 這樣你就可以測試你的函數環境中發生了什麼。我希望這能解決你的問題。