我正在使用SWI prolog,我的任务之一是使用Prolog实现一个简单的影院入门程序。一切似乎都运转正常,但问题是“你是否有成年人陪伴?”用户在12岁时推出'否'后,它应该只写一部电影,但它会写两部。
这是我到目前为止所做的:
% Author:
% Date: 01/02/2016
%cust(Name,Age)
filmlisting:- write('Film Showings: '),nl,
write(' - How to Lose Friends and Alienate People (15)'),nl,
write(' - Death Race (15)'),nl,
write(' - Space Chimps (5)'),nl,
write(' - The Chaser (18)'),nl,
write(' - Get Smart (12)'),nl, %12A
write('what is your age: '),nl,
read(Age),nl,
age(Age),nl.
age(5):-
write('Film that are available for your age: '),nl,
write(' - space chimps').
age(12):-write('Are you accomponied by adult '),nl,
read(Yes),
write('Films that are available for your age '),nl,
write(' - Space Chimps'),nl,
write(' - Get Smart').
age(12):-write('Are you accomponied by adult '),nl,
read(No),
write('Films that are available for your age '),nl,
write(' - Space Chimps').
age(15):-write('Films that are available for your age: '),nl,
write(' - How to Lose Friends and Alienate People'),nl,
write(' - Death Race').
age(18):-write('Films that are available for your age: '),nl,
write(' - The Chaser').
%films
film('How to Lose Friends and Alienate People').
film('Death Race').
film('Space Chimps').
film('The Chaser').
film('Get Smart').
%ages
age(5).
age(12).
age(15).
age(18).
%age rating
agerating(5).%u
agerating(12).
agerating(15).
agerating(18).
%film and age rating combined
film_agerating('DeathRace',(15)).
film_agerating('How to Lose Friends and Alienate People',(15)).
film_agerating('Space Chimps',(5)).
film_agerating('The Chaser',(18)).
film_agerating('Get Smart',(12)).%12A
%sub categorising the films into different caterogies
get_kidsfilm:-
film_agerating(X,5),
format('~w ~s kids film ~n',[X," - "]).
get_supervisionfilm:-
film_agerating(X,12),
format('~w ~s supervisional film ~n',[X," - "]).
get_teenagefilm:-
film_agerating(X,15),
format('~w ~s teenage film ~n',[X," - "]).
get_adultfilm:-
film_agerating(X,18),
format('~w ~s adult film ~n',[X," - "]).
get_allfilms:-
film_agerating(X,18),
format('~w ~s 18 ~n',[X," - "]),
film_agerating(Y,15),
format('~w ~s 15 ~n',[Y," - "]),
film_agerating(Z,12),
format('~w ~s 12 ~n',[Z," - "]),
film_agerating(A,5),
format('~w ~s U ~n',[A," - "]).
基本上这个问题就在这里,当用户已经进入'12'年龄并且'是'时,如果你是成年人陪同,它应该写两部适合他这个年龄的电影。但是,如果用户输入“否”,那么它应该只显示一部电影。 但!由于某种原因,它一直在显示两部电影,而不是一部。
响应:
filmlisting。 电影放映: - 如何失去朋友和疏远人(15) - 死亡竞赛(15) - 太空黑猩猩(5) - 追逐者(18) - 变得聪明(12) 你几岁: |:12。
你是成年人的伴侣吗? |:是的。 适合您年龄的电影 - 太空黑猩猩 - 变得聪明
filmlisting。 电影放映: - 如何失去朋友和疏远人(15) - 死亡竞赛(15) - 太空黑猩猩(5) - 追逐者(18) - 变得聪明(12) 你几岁: |:12。
你是成年人的伴侣吗? |:不。 适合您年龄的电影 - 太空黑猩猩 - 变得聪明
见No,仍然显示两个答案。
如果任何人可以提供任何帮助真的很棒。 谢谢。
答案 0 :(得分:1)
你误解了混凝土原子的变量。编译程序时,SWI-Prolog已经告诉你:
Warning: /var/folders/c8/t3slw2096zddgymjw5zwq1tc0000gn/T/ediprolog1064499_:21: Singleton variables: [Yes] Warning: /var/folders/c8/t3slw2096zddgymjw5zwq1tc0000gn/T/ediprolog1064499_:27: Singleton variables: [No]
将这些变量更改为原子yes
和no
,您的程序将按预期运行。
更重要的一点:您的计划目前在很大程度上取决于副作用。尝试找到一种更具说明性的方式来描述互动。这将允许您为您的程序实际编写测试用例!将声明性推理与实际IO分开,以获得可在更多方向上使用的解决方案。
例如,真的很难找到哪些电影需要成年人陪伴?如果您为可用知识选择合适的表示,这将成为可能。