0

在下面的代码中,我想找到一种首先读取字符的方法(通过用户输入“3.”,然后从 listCharacters 数据库
中删除/撤回该字符的数量

 :- dynamic listCharacters/1.  

 listCharacters(Joe).  
 listCharacters(Tom).  
 listCharacters(Peter).  

 :- write_ln('Type in the name of the character you have from the below list.    
  Example "Tom" '), write_ln('1. Joe'), write_ln('2. Tom'), write_ln('3. Peter'),   
  read(X), retract(listOfCharacters(X)).  
4

1 回答 1

0

首先,您应该阅读列表中的元素,然后根据该内容构建菜单,例如

:- dynamic listCharacters/1.

listCharacters('Joe').
listCharacters('Tom').
listCharacters('Peter').

menu :-
    findall(Elem, listCharacters(Elem), L),

    format('Type in the index of the character you have from the below list.~n', []),
    forall(nth1(I,L,E), format('~d. ~q~n', [I, E])),

    read(X),
    (   nth1(X,L,E),
        retract(listCharacters(E))
    ->  true
    ;   format('cannot remove ~q', [X])
    ).

我已经介绍了一个menu程序,如果您坚持调用它,请:- menu.在文件底部添加咨询。一些测试:

?- menu.
Type in the index of the character you have from the below list.
1. 'Joe'
2. 'Tom'
3. 'Peter'
|: 2.
true.

?- menu.
Type in the index of the character you have from the below list.
1. 'Joe'
2. 'Peter'
|: 1.
true.
于 2013-01-11T08:15:27.173 回答