【发布时间】:2018-11-26 00:14:32
【问题描述】:
我是 Prolog 课程的初学者,我正在尝试编写代码让用户选择咖啡,然后询问冷热咖啡的大小来计算价格。我在网上寻找如何开发程序的解释,但我觉得它与我在示例中需要的不同:[动物识别][1]。你能帮我写咖啡菜单吗?
这是我尝试过的。
go :- hypothesize(Coffee),
write('Your order is : '),
write(Coffee),
write('and the price for your order = : ')
nl,
undo.
/* hypotheses to be tested */
hypothesize(moca) :- moca, !.
hypothesize(hotChocolate) :- hotChocolate, !.
hypothesize(latte) :- latte, !.
hypothesize(cappuccino) :- cappuccino, !.
/* rules */
moca :-
/* ask if you want hot or cold
* ask the size of the coffee*/
我的方法是正确还是更好地创建一个列表,然后用户通过键入咖啡的名称来选择?
像这样添加菜单
menu :- repeat,
write('pleaase, Choose the Coffe to order:'),nl,
write('1. Moca'),nl,
write('2. Latte'),nl,
write('3. Hot Choclate'),nl,
write('Enter your choice number please: '),nl,
read(Choice),
run_opt(Choice).
【问题讨论】:
-
您好,欢迎来到 StackOverflow。这是为了家庭作业还是自己做的?如果这是供您自己使用,您可以通过放弃输入提示并首先专注于逻辑来使生活更轻松。
-
感谢您的回复,这是家庭作业。我应该如何专注于逻辑?
-
作为家庭作业,如果您放弃作业以便我们了解要求,将会很有帮助。我认为您有一个选择列表和每个选择的价格,甚至可能是取决于两个或多个选项的价格。
-
当我编写 Prolog 代码时,我倾向于先从低级逻辑开始,如果需要,再到用户界面。在这种情况下,它是必需的。
-
基本上这个逻辑会接受一个选项列表,遍历列表来调整价格并返回一个最终价格。然后,您可以添加用户界面来收集选项列表,然后输出最终价格。