用户输入,我们该怎么做? [英] User Input , How can we do it?

查看:43
本文介绍了用户输入,我们该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在 prolog 中从用户那里得到一些东西:例如:

How can we get something from user in prolog : for example :

animal(dog).
animal(cat).
write('please type animal name:'),nl.
/* How to read from user and store it to X 
and then check that user has typed animal name ?*/
?-animal(X).

推荐答案

您可以使用 read 来解决这个问题.例如,您可以将 read(X),animal(X). 写入 prolog 解释器或将其写入脚本文件:

You can use read for that. For example you could write read(X), animal(X). into the prolog interpreter or write this into a script file:

:- read(X), animal(X).

如果你在提示中输入一个有效的动物名称,它将被绑定到 X.如果你输入一个无效的名称,它不会.

If you then enter a valid animal name into the prompt, it will be bound to X. If you enter an invalid name, it won't.

或者你可以定义一个这样的过程:

Or you could define a procedure like this:

read_animal(X) :-
  write('please type animal name:'),
  nl,
  read(X),
  animal(X).

然后在解释器中调用它,如 read_animal(X)..

And then call it in the in the interpreter like read_animal(X)..

注意输入需要以.结束.

这篇关于用户输入,我们该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆