【发布时间】:2010-09-16 03:27:29
【问题描述】:
这是目前为止的完整代码:
module clean
#light
open System
open System.IO
let pause() = Console.ReadLine()
let drive = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory())
printfn "You're using the %s drive.\n\n" drive
let game1 = "Assassin's Creed"
let game2 = "Crysis"
let game3 = "Mass Effect"
let local1 = "\%APPDATA\%\\Ubisoft\\Assassin's Creed\\Saved Games\\"
let local2 = "\%USERPROFILE\%\\Documents\\My Games\\Crysis\\SaveGames\\"
let local3 = "\%USERPROFILE\%\\Documents\\BioWare\\Mass Effect\\Save\\"
let roam1 = drive + "Saves\\Abraxas\\" + game1 + "\\"
let roam2 = drive + "Saves\\Abraxas\\" + game2 + "\\"
let roam3 = drive + "Saves\\Abraxas\\" + game3 + "\\"
let rec getGame() =
printfn "Which Game?\n\n 1.%s\n 2.%s\n 3.%s\n\n" game1 game2 game3
match Int32.TryParse(stdin.ReadLine()) with
| true,1 -> game1
| true,2 -> game2
| true,3 -> game3
| _ ->
printfn "You did not enter a valid choice."
let _ = pause()
Console.Clear()
getGame()
let mutable gameprint = getGame()
printf "You have chosen %s\n\n" gameprint
let roaming =
match gameprint with
| game1 -> roam1
| game2 -> roam2
| game3 -> roam3
| _ -> "test"
printf "Roaming set to %s\n\n" roaming
let local =
match gameprint with
| game1 -> local1
| game2 -> local2
| game3 -> local3
| _ -> "test"
printf "Local set to %s\n\n" local
printf "Check gameprint %s" gameprint
在设置漫游和本地对象的部分中,它告诉我它永远不会与“game1”以外的任何东西匹配。
我在匹配本地和漫游对象之前和之后执行了“printf”检查...游戏打印在两个 printf 命令中都正确显示,但与 game1 以外的任何内容都不匹配...我'我不确定我在哪里犯了错误。
【问题讨论】:
-
这应该还是坚持DRY原则。我不会在任何地方重复任何字符串声明(我没有将 gamen 嵌入到 localn 中,因为在 localn 中发生变化的不仅仅是 gamen,不同的游戏很少有类似的结构,如您所见)。 DRY 原则的一部分:“逻辑上相关的元素都可预测且一致地变化”。如果要更改,Localn 永远不会发生可预测的更改。
标签: f# string-matching