如何从SWI-Promog中的用户输入中获取全名并将其显示为输出?

发布于 2025-01-21 10:35:24 字数 478 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2025-01-28 10:35:24

我刚刚发现了解决方案,这真的很简单。全名是字符的组合,因此我需要在输入中的名称的开头和末尾放置单引号标记。我不知道这是我的无知,还是我觉得这种事情很少在其他指南中解释。

输入应该看起来像这样。

?- name.
Write your full name: 'First Middle Last'.
First Middle Last
true.

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.

?- name.
Write your full name: 'First Middle Last'.
First Middle Last
true.
反话 2025-01-28 10:35:24

目标读取(x)从当前输入流读取下一个术语,并用x将其统一。为了使谓词正确读取全名,您需要键入单个引号中包含的名称,并使用 ofiper 完成输入。例如:

?- name.

Write your full name: 'First Middle Last'.
First Middle Last
true.

您还可以使用谓词 /code> ,在这种情况下,不应键入引号和期间:

name :-
    nl,
    write('Write your full name: '),
    read_line_to_string(user_input, FName),
    write(FName).

示例:

?- name.

Write your full name: First Niddle Last
First Niddle Last
true.

The goal read(X) reads the next term from the current input stream and unify it with X. 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:

?- name.

Write your full name: 'First Middle Last'.
First Middle Last
true.

You can also use the predicate read_line_to_string/2, in which case the quotes and period should not be typed:

name :-
    nl,
    write('Write your full name: '),
    read_line_to_string(user_input, FName),
    write(FName).

Example:

?- name.

Write your full name: First Niddle Last
First Niddle Last
true.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文