脚本应该分配并列出特定食物的数量。 所以我想要一个脚本来制作食物的“计划”,我将在本周吃。 我有一个可以“从200餐中选择”的柜台。 所以我需要的是它是这样的:
number * = Tacos
数字* =披萨
......像这样继续
我不知道怎么做...我希望你能帮助我! 这是我的代码,直到现在。
# Food selector for the week!
#random Stuff mixed for every day.
function Random-Food{
Param(
[array]$InputList
)
return $InputList | Get-Random -Count 7
$InputList.Count;
}
$a = 1..200
Write-Output (Random-Food -InputList $a)
答案 0 :(得分:3)
在单独的文件中,创建一个枚举(为了便于阅读):
Enum Food
{
Tacos
Pizza
...
}
在您的脚本中,点源此文件,以便您可以使用它:. 'C:\myenum.ps1'
然后在你的函数中:
function Get-Food {
[Enum]::GetValues([Food]) | Get-Random -Count 7
}
这将从您的列表中返回7种随机食物。作为额外的奖励,您可以为每种食物分配一个数字并获得该参考。 (Tacos = 5
并像[Food]::Tacos
一样访问它,可以将其视为字符串或整数)
包括当天:
function Get-Food {
$foodsOfWeek = [Enum]::GetValues([Food]) | Get-Random -Count 7
foreach ($day in [Enum]::GetValues([DayOfWeek])) {
([string]$day).Substring(0, 3) + ': ' + $foodsOfWeek[$day]
}
}
解释:
该函数将从您的自定义[Food]
枚举中获取所有值(Tacos,Pizza等),然后随机选择其中的7个以放入我们存储到$foodsOfWeek
的数组中。
此时,我们需要遍历一周中的几天(系统内置枚举[DayOfWeek]
)。我们将字符串转换为字符串,以便我们可以调用.Substring()
方法来获取前3个字符并附加:
进行格式化。
最后,我们使用$foodOfWeek
枚举访问[DayOfWeek]
数组,因为天数与数字0-6(数组的大小)相关。
(DayOfWeek.Sunday == 0) && (DayOfWeek.Sunday == "Sunday")) == true
答案 1 :(得分:0)
要了解PowerShell,请尝试以下操作:Windows PowerShell 3.0 Step by Step - Pearsoncmg.com
免费赠送。使用PowerShell 4
在Win 7机器上编写和测试。我加了一些食物。您所要做的就是将项目添加到$foodList
。
示例输出遵循代码
cls
#define an array of strings and load it with food words
$foodList = @("Apples","Bananas", "Cherries","Grapes","Mangoes","Melons","Oranges","Peaches","Pears","Pineapples","Strawberries","Tomatoes")
#define an array of days
$dow = @("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
#loop 7 times, once per day and generate a random number between 0 and the number of food items - 1
for ($i = 0; $i -lt 7; $i++)
{
$foodItemIndex = Get-Random -Minimum 0 -Maximum ($foodList.Count - 1)
write-host ($dow[$i] + ": " + $foodList[$foodItemIndex])
}
答案 2 :(得分:0)
这是一种方法:
$foodChoices = "Pizza","Taco","Dim Sum","Burger","Salad","Soup","Sandwich","Curry"
$foodChoices[(Get-Random -InputObject (0..($foodChoices.Count - 1)) -Count 7)] |
ForEach-Object {$daysOfWeek = [enum]::GetNames([DayOfWeek]); $day=0}{
[PsCustomObject]@{
Day=$daysOfWeek[$day++]
Food=$_
}
}
输出看起来像这样:
Day Food
--- ----
Sunday Burger
Monday Sandwich
Tuesday Salad
Wednesday Taco
Thursday Soup
Friday Dim Sum
Saturday Curry
只需向$foodChoices
添加更多项目,您就可以开始......