是否有任何flush(stdin)像在c++中清除缓冲区在f# ?



目前我正在学习一门新的程序语言,它是f#。现在,我正在做一个关于订餐系统的项目。而且,我目前遇到的一个问题是当我完成所有的订购后,系统会提示出一个语句说"你想继续购买吗?"然后我输入与条件相关的小y和大y,但系统直接给我"谢谢您的购买";按任意键继续语句。有人有什么办法来处理这个问题吗?谢谢,谢谢。

open System
let menu()=
printfn "---------------------------------------"
printfn "        Food ordering system"
printfn "---------------------------------------"
printfn "1. ORDER FOOD "
printfn "2. ORDER DRINK"   
Console.Write("Enter your selection : ")

let loop()=
Console.Write("Do you want to continue pruchase?:")
let choice=Console.ReadLine()
if(choice="y"||choice="Y")then
menu()
let food()=
printfn "n1.MCD CHICKEN RM 10 n2.FILET-O-FISH RM 13 n3.FRENCH FRIES RM 5 n4.APPLE PIE RM 4"
Console.Write("nEnter food code that you want :")
let option=Console.ReadLine()
Console.Write("How many you want :")
let qty=Console.ReadLine()
if(option = "1")then
let price = 10
let total = int qty*int price
Console.Write("nThe price for {0} set MCD CHICKEN ",qty)
Console.Write("is RM {0} n",total)
loop()
Console.Write("nThank you for your purchase ")
elif(option = "2")then
let price = 13
let total = int qty*int price
Console.Write("nThe price for {0} set FILET-O-FISH ",qty)
Console.Write("is RM {0} n",total)
loop()
Console.Write("nThank you for your purchase ")
elif(option = "3")then
let price = 5
let total = int qty*int price
Console.Write("nThe price for {0} FRENCH FRIES ",qty)
Console.Write("is RM {0} n",total)
loop()
Console.Write("nThank you for your purchase ")
elif(option = "4")then
let price = 4
let total = int qty*int price
Console.Write("nThe price for {0} APPLE PIE ",qty)
Console.Write("is RM {0} n",total)
loop()
Console.Write("nThank you for your purchase ")
else
printfn "Invalid input"
menu()
let drink()=
printfn "n1.ICE BLENDED CHOCOLATE MEDIUM RM 8 n2.MANGO PEACH RM 11 n3.PEPSI RM 4 n4.ICE LEMON TEA RM 4"
Console.Write("nEnter beverage code that you want :")
let option=Console.ReadLine()
Console.Write("How many you want :")
let qty=Console.ReadLine()
if(option = "1")then
let price = 8
let total = int qty*int price
Console.Write("nThe price for {0} ICE BLENDED CHOCOLATE MEDIUM ",qty)
Console.Write("is RM {0} n",total)
loop()
elif(option = "2")then
let price = 11
let total = int qty*int price
Console.Write("nThe price for {0} MANGO PEACH ",qty)
Console.Write("is RM {0} n",total)
loop()
Console.Write("nThank you for your purchase ")
elif(option = "3")then
let price = 4
let total = int qty*int price
Console.Write("nThe price for {0} PEPSI ",qty)
Console.Write("is RM {0} n",total)
loop()
Console.Write("nThank you for your purchase ")
elif(option = "4")then
let price = 4
let total = int qty*int price
Console.Write("nThe price for {0} ICE LEMON TEA ",qty)
Console.Write("is RM {0} n",total)
loop()
Console.Write("nThank you for your purchase ")
else
printfn "Invalid input"
menu()

let main()=
menu()
let selection=Console.ReadLine()
if(selection = "1") then
food()
else
drink()
main()

这不是刷新缓冲区的问题。您正在从loop调用menu,但在继续之前没有等待用户的输入。

解决这个问题的一种方法是将ReadLine调用移动到menu,但随后您将最终从menu递归地调用fooddrink,这已经在fooddrink中从loop调用。这会创建嵌套的控制流,这可能不是您真正想要的。

我建议重新考虑设计,以便不同级别之间的依赖关系是干净的。每个函数应该返回一个被调用它的函数使用的值,而不是包含返回到菜单的循环。

老实说,这段代码在很多方面都太命令式了,而且不符合f#的习惯。

我谦卑地建议你将这段代码发布到https://codereview.stackexchange.com/,并从那里获得代码审查,从中学习。

相关内容

  • 没有找到相关文章

最新更新