当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 星期五乐趣:我和乐队在一起。

作者:精品下载站 日期:2024-12-14 07:44:21 浏览:12 分类:玩电脑

星期五乐趣:我和乐队在一起。


[玩转系统] 星期五乐趣:我和乐队在一起。

如果你还没弄清楚,摇滚就是今天的主题。我整理了一个摇滚小测验。在许多摇滚乐队中,至少有一位成员是众所周知的。如果有人说“Axl Rose”,您很可能会知道 Guns n’ Roses。但你认识乐队的其他成员吗?我创建了一个包含许多知名摇滚乐队的 XML 文档。由于乐队成员发生了变化,我尝试使用乐队巅峰时期的阵容。如果您想玩,您需要下载 BandData.xml。将其作为 XML 文件保存到与脚本相同的目录中,稍后我将向您展示。尽量不要过多地查看内容。这是结构。

<Bands>
  <Band>
    <Name></Name>
    <Lead></Lead>
    <Members>
      <Member></Member>
      <Member></Member>
    </Members>
  </Band>
</Bands>

我的测验是一个处理 XML 文档中的数据的 PowerShell 脚本。它将显示乐队成员列表,没有可识别的领导和可能乐队的多种选择。回答完问题后,您将受到评判,我的意思是评分。


#BandQuiz.ps1

Param($Questions = 10)

$file = '.\BandData.xml'
[xml]$data = Get-Content $file

#get all of the band names
$bandNames = $data.Bands.Band.name

#select 10 random entries
$testdata =  $data.Bands.band | Get-Random -Count $Questions

#initialize some counter variables that will be updated from a different scope
$script:i = 0
$script:q = 0

#process each one
foreach ($band in $testdata) {

    #build a list of band names
    $answer = $band.name
    $Choices = $bandNames | where {$_ -ne $answer} | Get-Random -count 3
    $Choices+= $answer

    #sort names and create a hashtable using a number as the key
    $bandhash = @{}
    $choices | Sort | foreach -begin {$K=1} -process {
      $bandhash.Add($K,$_)
      $K++
    }
   
    #get band members as a string list
    $members = ($band.members | select -expand member | Format-List | Out-String).Trim()

    #prompt
    #use a scriptblock as an ad-hoc function
$promptblock = {
Param([string]$Lead)

if ($lead) {$leadmember = "*$lead*`n"}
#cls
$script:Q++

#define a here string to be used for the prompt
$prompt=@"
Question: $script:Q

$members
$leadmember
What band do these people belong to?"

$(($bandHash.GetEnumerator() | Sort Name | out-string).trim())

Enter a number or 0 to see the lead band member
"@

    [int]$r = Read-Host -Prompt $prompt

    #check result
    if ($r -eq 0) {
      $Band.lead
      #decrement question counter since we're repeating
      $script:Q--
      #re-run the prompt scriptblock to include the lead member
      &$promptblock -lead $band.Lead
    }
    elseif ($bandhash.$r -eq $answer) {
        Write-Host "Correct!" -ForegroundColor green
        #record result
        $script:i++
    }
    else {
        Write-Host "Incorrect" -ForegroundColor red
    }
} #close promptblock
 &$promptblock
}

#display results
[int]$correct = ($i/$Questions) * 100

Switch ($correct) {
{$_ -ge 90} {$quip = "You are a juke box hero." ; break}
{$_ -ge 75} {$quip = "You're bad to the bone." ; break}
{$_ -ge 50} {$quip = "It's a long way to the top if you wanna rock 'n roll." ; break}
{$_ -ge 25} {$quip = "Another one bites the dust." ; break}

default {$quip = "Stick to Mantovani."}

}

"`nYou got {0:P0} correct. {1}" -f ($i/$Questions),$Quip

我们来看看剧本的几个要点。

首先,我需要加载 XML 文档。

$file = '.\BandData.xml'
[xml]$data = Get-Content $file

[XML] 类型加速器将创建一个 XML 文档。当您在 PowerShell 中拥有 XML 文档时,每个节点都可以被视为一个属性,因此可以非常轻松地导航或获取值,例如所有乐队名称的列表。

$bandNames = $data.Bands.Band.name

然后,该脚本从 XML 文档中选择随机数量的频段条目。这些将成为测验的基础。对于每个项目,我都会创建一个将显示的乐队选择和乐队成员的列表。您还会注意到我使用 $script 前缀初始化了一些计数器。

$script:i = 0
$script:q = 0

原因如下。我正在使用定义为 $promptblock 的脚本块来显示每个问题并跟踪正确答案。脚本块在新的范围或容器中运行。这意味着当它尝试对像 $Q 这样的变量执行某些操作时,它首先会在当前范围中查找该项目。如果它找到它,它就会使用它。否则,PowerShell 将在范围层次结构中向上搜索到父范围以查找该项目。但这就是让人困惑的地方。如果您像我一样只是阅读 $bandhash 对象之类的内容,PowerShell 会很乐意在父作用域中找到它并显示它。但是当我尝试修改像 $Q 或 $i 这样的变量时,它只能在当前范围内修改它。但我需要在脚本块范围之外使用这些变量,因此我在变量前面加上 $Script: 来指示这些变量的范围级别。一般规则是不要引用超出范围的变量,但由于我使用的是 $script:我告诉 PowerShell 我知道我在做什么。

完成所有问题后,脚本可以计算您的正确答案数量并显示记分卡。我决定使用 Switch 语句来提供帮助。

Switch ($correct) {
{$_ -ge 90} {$quip = "You are a juke box hero." ; break}
{$_ -ge 75} {$quip = "You're bad to the bone." ; break}
{$_ -ge 50} {$quip = "It's a long way to the top if you wanna rock 'n roll." ; break}
{$_ -ge 25} {$quip = "Another one bites the dust." ; break}

default {$quip = "Stick to Mantovani."}

}

通常在 Switch 中您会使用一个简单的值。但您也可以使用 PowerShell 表达式。在我的 Switch 语句中,如果 $Correct 的值 >= 90,则我为 $quip 分配特定值。使用表达式时,请使用 $_。请记住,Switch 将处理每个匹配的表达式,因为我不希望这样,所以我使用 Break 关键字,这样 PowerShell 就知道不要继续检查其他可能性。

当您运行测验时,您将得到如下项目:

[玩转系统] 星期五乐趣:我和乐队在一起。

显示来自提示脚本块。如果您需要一点帮助,请输入 0,这一次会重新显示问题,并带有(希望)更容易识别的线索。

[玩转系统] 星期五乐趣:我和乐队在一起。

最后是对你的摇滚知识的尖刻评论。

[玩转系统] 星期五乐趣:我和乐队在一起。

因为我已经到了一定的年龄,所以我的乐队数据 xml 文件的内容可能会略有偏差。如果您出生于 1985 年之后,您可能会遇到一些问题。

我认为 XML 文件会让一些 IT 专业人员感到害怕,但一旦您了解了一些基础知识,使用它们实际上并不困难。事实上,我将在以后的帖子中回到我的乐队 xml 文件。同时,如果您对我的测验脚本有任何疑问,请继续参加并告诉我。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯