如何从SWI-Promog中的用户输入中获取全名并将其显示为输出?
我是Swi-Promog的新手,并且仍在学习。我正在制作一个简单的程序来询问用户的全名并显示输出。但是,我一直在为如何从用户输入中获得全名,以及一个名称,中名和姓氏之间的空格。当我尝试显示仅是名字的输出时,输出只是一个代码。
这是我到目前为止写的:
name:-nl,
write('Write your full name: '),read(FName),
write(FName).
这是输入和输出。 prolog_name_program
这是我想要的输出。
?- name.
Write your full name: First Middle Last.
First Middle Last
true.
I'm new to SWI-Prolog and still learning it. I'm making a simple program to ask for user's full name and display the output. However, I've been struggling on how to get a full name from the user input together with spaces between firstname, midname, and last name from a single input. When I try to display an output that is only the first name, the output is just a code.
Here's what I wrote so far :
name:-nl,
write('Write your full name: '),read(FName),
write(FName).
Here's the input and the output.
Prolog_Name_Program
This is the output that I want.
?- name.
Write your full name: First Middle Last.
First Middle Last
true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚发现了解决方案,这真的很简单。全名是字符的组合,因此我需要在输入中的名称的开头和末尾放置单引号标记。我不知道这是我的无知,还是我觉得这种事情很少在其他指南中解释。
输入应该看起来像这样。
I just discovered the solution and it's really simple. Full name is a combination of characters therefore I need to put single quotation mark at the beginning and at the end of the name in the input. I don't know if it is my ignorance or I feel like this kind of thing is rarely explained in other guides.
The input should look like this.
目标
读取(x)
从当前输入流读取下一个术语,并用x
将其统一。为了使谓词正确读取全名,您需要键入单个引号中包含的名称,并使用 ofiper 完成输入。例如:您还可以使用谓词 /code> ,在这种情况下,不应键入引号和期间:
示例:
The goal
read(X)
reads the next term from the current input stream and unify it withX
. In order for the predicate to correctly read a full name, you need to type that name enclosed in single quotes and finish the input with a period. For example:You can also use the predicate
read_line_to_string/2
, in which case the quotes and period should not be typed:Example: