0

我正在为手机维修创建一个专家系统。

答案必须只是 [yes or no],否则将显示“无效输入”。我怎样才能在我的代码中捕获它?

ask(Question) :-  write('Question: '),
      write(Question),
      write('? '),
      write('(yes or no) : '),
      read(Response),
      nl,
      ((Response == yes ; Response == y) -> assert(yes(Question)) ; 
       (Response==no ; Response ==n) -> assert(no(Question)) ;
        write('\nInvalid Input!!!\n'),fail).

如果输入任何拼写错误,我仍然无法实现我想要显示的内容。

4

1 回答 1

1

嗯......你的代码肯定有效。只要确保assert使用gprologassertaassertz使用 gprolog 进行更改。

| ?- [expert].
compiling *** for byte code...
*** compiled, 7 lines read - 2111 bytes written, 14 ms
(1 ms) yes
| ?- ask(man).
Question: man? (yes or no): y.
yes
| ?- ask(woman).
Question: woman? (yes or no): no.
(1 ms) yes
| ?- ask(silly).
Question: silly? (yes or no): dunno.
Invalid Input!!!
no
| ?- yes(man).
yes
| ?- no(man).
no
| ?- yes(woman).
no
| ?- no(woman).
(1 ms) yes
于 2013-02-09T09:30:28.213 回答