0

我有一个数字生成器的代码,它随机选择 1 到 30 之间的数字。我想使用批处理文件来选择每周的晚餐,所以我不必自己做。我的问题是我不知道如何将晚餐名称分配给数字,然后我希望它检查晚餐是否符合“晚餐评级”,这样可以确保晚餐足够健康,如果没有的话达到评级然后代码将/应该使它重新选择晚餐。这是数字生成器:(不要告诉我我可以做得更好,我有点菜鸟)

@echo off
echo.
echo             Welcome %username%
ping localhost -n 2 >nul
echo        This Is The Dinner Selector.
ping localhost -n 2 >nul
echo This Program Will Select The Weekly Dinner.
ping localhost -n 2 >nul
set /a selection1=%random% %%30 +1
set /a selection2=%random% %%30 +1
set /a selection3=%random% %%30 +1
set /a selection4=%random% %%30 +1
set /a selection5=%random% %%30 +1
echo %selection1%
echo %selection2%
echo %selection3%
echo %selection4%
echo %selection5%

请帮我找到解决问题的方法,我厌倦了自己挑选健康的晚餐。

4

2 回答 2

0

好的,不完全确定您想要什么,但这可能是:

主文件

@echo off
setlocal Enabledelayedexpansion
echo.
echo             Welcome %username%
sleep 2
echo        This Is The Dinner Selector.
sleep 2
echo This Program Will Select The Weekly Dinner.
sleep 2
Echo.
Echo.
Echo How healthy do you want your food [0 - Anything :: 9 - max]
choice /c "1234567890" /n /m ": " /d 0 /t 20
set rate=%errorlevel%
set /a count=0
set /a total=0
for /f %%a in (Dinner.txt) do (set /a total+=1)


:: START LOOP ----------------------
:choose
set /a count+=1
:random
set /a process=0
set sel=
set /a sel=%random% %% %total% + 1

for /f "tokens=1,2 delims=:" %%a in (Dinner.txt) do (
    set /a process+=1
    if !process! EQU !sel! (
        if %%b GEQ !rate! (
            set sel=%%a
            )
        )
    )

if "%sel%" EQU "" (Echo %sel%
) Else (
goto :random)

if count leq 5 goto choose
:: END LOOP   ----------------------

cls
Echo Enjoy your meal!
sleep 3
Exit

请注意,如果健康状况很高,这可能会导致重复和/或无限/非常长的循环。

晚餐.txt

这是您所有选择的列表。当批处理文件适应其大小时,它可以任意长/短。

里面一共有5个选择

; This is how you format the dinner file: [Food]:[Healthyness 0 - 9]
; Lines starting with semi-colons are ignored (if you want a blank line comment it)
; Examples:
;
; Vegetarian
Salad:9
Pasta:8
;
;
; Non-Veg
Uncooked Meat:2
Pizza:5
Burger:6
Pure Oil:0

我还没有对此进行测试,并且还可以保证会出现错误,因为它非常平凡且冗长,但我相信一些错误测试会完成这项工作

莫娜。

于 2013-11-12T09:40:56.797 回答
0

管理多个相同类型的元素(每个元素由索引标识)的方法是通过数组我强烈建议您在尝试在 Batch中使用数组之前了解此主题以及环境变量的延迟扩展。掌握基础知识后,您可以执行以下操作:

set dinner[1]=Name of first dinner
set dinner[2]=Name of second dinner
set dinner[3]=Name of third dinner, etc

或者,以更高级的方式:

set i=0
for %%a in (Pasta:9 Dessert:5 Soup:8) do (
   for /F "tokens=1,2 delims=:" %%b in ("%%a") do (
      set /A i+=1
      set dinner[!i!]=%%b
      set rating[!i!]=%%c
   )
)

例如:

set /A selection=%random% %% 30 + 1
if !rating[%selection%]! gtr 7 echo Rating of !dinner[%selection%]! correct: greater than 7
于 2013-11-12T21:00:12.687 回答