我怎样才能使 set /a X=1, 1 等于“Potato”?

我怎样才能使 set /a X=1, 1 等于“Potato”?

所以我问这个问题并不是因为它是重要信息或其他什么,我只是喜欢在无聊的时候用批处理脚本编程,并想创建一个可以为我打开应用程序的“个人助理”,这样可以让我的生活更轻松一点,而不是在我凌乱的电脑上搜索。

我想包含一个选择 /c MF /m /n 男性或女性,这样我就可以将它发送给我的朋友让他们也使用它,但我似乎无法让它正常工作,因为 %gender% 会得到 0 值而不是 M 或 F。

我尝试使用 set /p 但我想知道是否有办法使用 set 来使用文字而不是数字,谢谢!

(以下是我写的,以防你需要它,我知道对于研究过这个的人来说它可能看起来很简单或很容易,但我只是把它当作一种爱好和新手“程序员”来做)

echo Username
set /p username=
echo.
echo.
echo.
echo Gender
choice /c 12 /m /n 1: Male, 2: Female
if %errorlevel%==1 (set /a gender=1)
if %errorlevel%==2 /set /a gender=2)
echo.
echo.
echo.
echo Thanks, we will start to create a profile for you...
timeout /t 2 /nobreak >nul
cls
echo Creating profile...
cls
echo Please, insert the name the filesaves will have.
set /p savefile=
timeout /t 1 /nobreak >nul
cls
echo ----------------------------
echo Starting up...
(
echo %username%
)>%savefile%user.sgf
echo %username%
echo ----------------------------
timeout /t 2 /nobreak >nul
echo 50%
timeout /t 1 /nobreak >nul
(
echo %gender%
)>%savefile%gender.sgf
echo %gender%
echo ----------------------------

答案1

好的。好的。我明白了。你在学习(这真的很酷)

  • 已修复多个语法错误。请查看我和您做的不同之处。
  • 一种可以从“1/2”转变为“雄性/雌性”的方法 set /a只为数学
  • 你不需要所有这些括号......
  • 任何时候您在批处理中评估变量时,都要引用双方。if "bob"=="%myname%" echo Meh namez bob. 如果由于某种原因您的变量为空,则批处理文件将给您手指并退出。

一些方法可以使它变得更好!

  • 使用%用户配置文件%\SomeDangDir用于“保存文件”。现在,它们会放在当前目录所在的位置。
  • 弄清楚如何将性别、用户名和其他任何内容放入同一个文件中,然后再将其取出。这将是一个挑战,有 100 种不同的方法可以实现它。

`

@echo off
SetLocal

echo Input Username:
set /p username=

echo.
echo Input Gender
choice /c 12 /m "1: Male, 2: Female"

if "%errorlevel%"=="1" (set gender=male) else (set gender=female)

echo.
echo.
echo username=%username%
echo gender=%gender%
echo Thanks, we will start to create a profile for you...

timeout /t 2 /nobreak >nul
cls

echo Creating profile...
timeout /t 1 /nobreak >nul
cls

echo Please, insert the name the filesaves will have:
set /p savefile=

cls
echo ----------------------------
echo Starting up...
echo %username%>%savefile%user.sgf
echo %username%
echo ----------------------------
timeout /t 2 /nobreak >nul
echo 50%

timeout /t 1 /nobreak >nul
echo %gender%>%savefile%gender.sgf
echo %gender%
echo ----------------------------

EndLocal

祝你好运! :)

相关内容