PowerShell脚本开发需求:实现200种餐食编号映射与周饮食计划
Hey there! Let's get your script to properly link food items with their numbers and generate a usable weekly meal plan. Your current code generates random numbers from 1-200, but we need to tie those numbers to actual food names first. Here's a step-by-step solution:
1. Create a Mapping of Numbers to Food Items
First, we need to associate each number (1 to 200) with a specific food. The easiest way to do this is with a hashtable (a key-value pair structure), where the key is the number and the value is the food name. Alternatively, you can use an array (since arrays are zero-indexed, we'll just add 1 to the index to get the number).
Here's how to set this up:
# Replace this example list with your full 200 food items $allFoods = @( "塔可", "披萨", "汉堡", "凯撒沙拉", "番茄意面", "日式寿司", "牛肉拉面", # ... keep adding until you have 200 items "水果燕麦碗" ) # Create a hashtable to map numbers (1-200) to foods $numberedFoods = @{} for ($index = 0; $index -lt $allFoods.Count; $index++) { $foodNumber = $index + 1 # Start numbering at 1 $numberedFoods[$foodNumber] = $allFoods[$index] }
If your food list is stored in a text file (one food per line), you can load it like this instead:
$allFoods = Get-Content -Path "C:\Path\To\Your\FoodList.txt"
2. Update the Random Selection Function
Next, we'll rewrite your function to pick 7 unique random numbers, then pair each number with its corresponding food and format it as a weekly plan (with days of the week for clarity):
function Get-WeeklyMealPlan { Param( [hashtable]$FoodMapping ) # Pick 7 unique random food numbers from the mapping $selectedNumbers = $FoodMapping.Keys | Get-Random -Count 7 # Pair numbers with foods and assign to days of the week $days = @("周一", "周二", "周三", "周四", "周五", "周六", "周日") $mealPlan = @() for ($i = 0; $i -lt 7; $i++) { $mealPlan += [PSCustomObject]@{ 日期 = $days[$i] 餐食编号 = $selectedNumbers[$i] 餐食名称 = $FoodMapping[$selectedNumbers[$i]] } } return $mealPlan }
3. Generate and Display the Plan
Finally, call the function and output the result in a readable format:
# Generate the weekly plan $weeklyMeals = Get-WeeklyMealPlan -FoodMapping $numberedFoods # Print the plan Write-Host "📅 本周餐食计划:" -ForegroundColor Cyan $weeklyMeals | Format-Table -AutoSize
Bonus: Quick Array Alternative
If you prefer using an array instead of a hashtable (since your numbers are sequential), you can simplify the selection step. Just pick a random index, add 1 to get the number, and grab the food directly:
function Get-WeeklyMealPlan { Param( [array]$FoodList ) $selectedIndices = 0..($FoodList.Count - 1) | Get-Random -Count 7 $days = @("周一", "周二", "周三", "周四", "周五", "周六", "周日") $mealPlan = @() for ($i = 0; $i -lt 7; $i++) { $foodNumber = $selectedIndices[$i] + 1 $mealPlan += [PSCustomObject]@{ 日期 = $days[$i] 餐食编号 = $foodNumber 餐食名称 = $FoodList[$selectedIndices[$i]] } } return $mealPlan } # Call it with your food array $weeklyMeals = Get-WeeklyMealPlan -FoodList $allFoods
Now your script will generate a clear, numbered weekly meal plan with actual food names instead of just random numbers!
内容的提问来源于stack exchange,提问作者alex

