剪辑 - 如何从 IF/READ 语句的异常中排除字符?



代码向用户询问y/n问题,并进行简单的更改。该语句似乎只接受整数和浮点类型,我只需要两个答案,所以我使用了1和0,并排除了其余的,但它只读取数字,所以只排除数字,而不排除字符。

(defrule rule01
=>
(printout t "Question (yes=1/no=0)?" crlf)
(bind ?x (read))
(if (!= ?x 1)
then
(if (= ?x 0)
then
(assert (rule01 no))
else (printout t "Use ONLY 0 OR 1 for your answers!" crlf))
else (assert (rule01 yes))))

目前,当您尝试键入字符时,它会返回以下内容:

CLIPS> (run)
Question (yes=1/no=0)?
g
[ARGACCES5] Function <> expected argument #1 to be of type integer or float
[PRCCODE4] Execution halted during the actions of defrule rule01.

如何为字符添加异常?

使用eq和neq代替=和<>。

CLIPS (6.31 6/12/19)
CLIPS> 
(defrule rule01
=>
(printout t "Question (yes=1/no=0)?" crlf)
(bind ?x (read))
(if (neq ?x 1)
then
(if (eq ?x 0)
then
(assert (rule01 no))
else (printout t "Use ONLY 0 OR 1 for your answers!" crlf))
else (assert (rule01 yes))))
CLIPS> (reset)
CLIPS> (run)
Question (yes=1/no=0)?
g
Use ONLY 0 OR 1 for your answers!
CLIPS> 

最新更新