批量颜色选择错误

时间:2017-04-11 16:24:30

标签: windows batch-file

我遇到了批处理程序的问题,我试图让用户输入选择的颜色。问题是,当我输入我想要的值并按下输入程序关闭。我有Windows 10 64x

@echo off

:setcolor
set /p %color%= "What color text do you want? [0-White  1-Blue  2-Green  3-Red  4-Yellow]"
if %color% EQU 0 goto :1 
if %color% EQU 1 goto :2
if %color% EQU 2 goto :3
if %color% EQU 3 goto :4
if %color% EQU 4 goto :5
goto :setcolor

:1
color 07
goto :main 
:2
color 01
goto :main
:3
color 02
goto :main
:4
color 0c
goto :main
:5
color 06
goto :main

:main
echo test
pause

1 个答案:

答案 0 :(得分:0)

正如@Stephan所提到的,set /p %color%=应更改为set /p color=

设置变量时,我们不会在变量周围放置%(展开它),但我们输入变量的名称。

所以,这是你的代码:

@echo off

:setcolor
set /p color="What color text do you want? [0-White  1-Blue  2-Green  3-Red  4-Yellow]"
if "%color%" EQU "0" goto :1 
if "%color%" EQU "1" goto :2
if "%color%" EQU "2" goto :3
if "%color%" EQU "3" goto :4
if "%color%" EQU "4" goto :5
goto :setcolor

    .....

顺便说一句,我在"添加引号(%color%)和数字以防止批处理文件在遇到空输入或带空格的输入时停止