我已經writen這個功能與幾個測試案例:功能讓我總是尾隨`NULL`回
characterCounter <- function(char1, char2) {
if(is.null(char1) || is.null(char2)) {
print("Please check your character sequences!")
return()
}
if(nchar(char1, type = "chars") < nchar(char2, type = "chars") || nchar(char1, type = "chars") <= nchar(char2, type = "chars")) {
cat(sprintf("%s is smaller or smaller-equal than %s\n", char1 , char2))
return()
} else if(nchar(char1, type = "chars") > nchar(char2, type = "chars") || nchar(char1, type = "chars") >= nchar(char2, type = "chars")) {
cat(sprintf("%s is greater or greater-equal than %s\n", char1 , char2))
return()
} else if(nchar(char1, type = "chars") == nchar(char2, type = "chars")) {
cat(sprintf("%s is equal to %s\n", char1, char2))
return()
}
}
#Testcases
(characterCounter("Hello","Hell"))
(characterCounter("Wor","World"))
然而,每個案例後,我得到的結果:
> (characterCounter("Hello","Hell"))
Hello is greater or greater-equal than Hell
NULL
> (characterCounter("Wor","World"))
Wor is smaller or smaller-equal than World
NULL
我也不是什麼就像我的輸出是尾隨NULL
。爲什麼我回來了? (characterCounter(NULL,NULL))
UPDATE
characterCounter <- function(char1, char2) {
if(is.null(char1) || is.null(char2)) {
return(cat("Please check your character sequences!"))
}
if(nchar(char1, type = "chars") < nchar(char2, type = "chars") || nchar(char1, type = "chars") <= nchar(char2, type = "chars")) {
return(cat(sprintf("%s is smaller or smaller-equal than %s\n", char1 , char2)))
} else if(nchar(char1, type = "chars") > nchar(char2, type = "chars") || nchar(char1, type = "chars") >= nchar(char2, type = "chars")) {
return(cat(sprintf("%s is greater or greater-equal than %s\n", char1 , char2)))
} else if(nchar(char1, type = "chars") == nchar(char2, type = "chars")) {
return(cat(sprintf("%s is equal to %s\n", char1, char2)))
}
}