在下面的代码中,我想找到一种首先阅读角色的方法 (通过用户键入“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)).
答案 0 :(得分: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.