用户输入,我们该怎么做?

编程入门 行业动态 更新时间:2024-10-28 14:29:06
本文介绍了用户输入,我们该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们如何在 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)..

注意输入需要以.结束.

更多推荐

用户输入,我们该怎么做?

本文发布于:2023-10-18 02:10:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502773.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:怎么做   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!