【问题标题】:JESS -> How to check the type of a variable and test it?JESS -> 如何检查变量的类型并进行测试?
【发布时间】:2013-02-13 09:57:55
【问题描述】:

我在 JESS(Java 专家系统 Shell)中有一个任务,但我遇到了一些问题。

我正在尝试检查某个变量(从键盘读取)是否为正整数(我已经设法检查它是否为正),但我找不到检查方法(或函数)如果变量是整数/数字。

我尝试了 intergerpnumberp 函数,但这些函数似乎不起作用。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我回答了,但我怀疑还会有更多内容。如果您修改您的问题以进一步解释为什么integerp 没有做您想做的事,我可以在我的回答中详细说明。

标签: types numbers integer jess


【解决方案1】:

integerp 确实是检查某事物是否为整数的方法,如下面的文字记录所示。

Jess> (bind ?x (read))
1
1
Jess> (integerp ?x)
TRUE
Jess> (bind ?y (read))
foo
foo
Jess> (integerp ?y)
FALSE
Jess> (bind ?z (read))
1.1
1.1
Jess> (integerp ?z)
FALSE

【讨论】:

  • 谢谢弗里德曼-希尔先生,我会仔细研究一下,看看为什么我的规则不起作用 :)
  • (defrule error (declare (auto-focus TRUE)) ?f (printout t "Please insert an integer" crlf) ) (defrule error (declare (auto-focus TRUE)) ?f (printout t "Please insert an integer" crlf)) 规则 I 有两种不同的尝试正在尝试做。我从 MAIN 模块中的键盘读取了某个值,如果此变量不是整数,则将触发此规则并要求用户再次插入该值。
  • 第一个是正确的,但是第二个是错误的;规则的左侧包含模式,而不是函数调用(“测试”在这里算作模式。)但是第一个应该可以正常工作,假设某些东西正在断言 kb 事实,其中有一个项目,从键盘读取的值。所以也许你应该告诉我这个事实是从哪里来的。
  • (defrule init => (printout t "Insert function number:" crlf) (bind ?fun (read)) (if (= ?fun 1) then (focus Somar)) (if (= ?fun 2) then (focus Subtrair)) (if (= ?fun 3) then (focus Quadrado)) (printout t "Insert integer:" crlf) (assert (kb (read)))) 我刚刚删除了一些(打印输出) 是为了使其更易于阅读,因为它不会改变规则行为。
  • 我花了很长时间才看到这一点,但问题出在另一条规则中——当(test (integerp ?x)) 为真时,您的规则匹配,但您想在(test (integerp ?x)) 为真时打印消息false,这一定是你看到的问题。只需更改模式以反转表达式的逻辑意义——即(test (not (integerp ?x)))
猜你喜欢
  • 2019-11-25
  • 2010-10-02
  • 1970-01-01
  • 2011-04-12
  • 2018-03-21
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 2021-01-13
相关资源
最近更新 更多