我正在尝试使用 swi-prolog 编写一个程序,该程序随机询问人们的名字或姓氏,并根据他们输入的内容打印“正确”或“不正确”。当前的正确答案是“Hello”和“World”,但无论用户键入什么,输出都是错误的,并且不会打印正确/错误。
start:-(Q=['What is your first name?','What is your last name?'],
random_member(N,[0,1]),
nth0(N,Q,X),
writeln(X)),
readln(S),
check_answer(N,S).
check_answer(N,S):-A=['Hello','World'],
nth0(N,A,X),
writeln(X),
(S=@=X)->writeln('correct'),
not(S=@=X)->writeln('incorrect').
我后来将其编辑为:
start:-(Q=['What is your first name?','What is your last name?'],
random_member(N,[0,1]),
nth0(N,Q,X),
writeln(X)),
read(S),
check_answer(N,S).
check_answer(N,S):-A=['Hello','World'],
nth0(N,A,X),
writeln(X),
writeln(S),
((S=@=X))->writeln('correct') ; writeln('incorrect').