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 に答える