【发布时间】:2014-09-06 08:21:18
【问题描述】:
我编写了一个批处理文件来向电话号码%number% 发送短信。 (如果您的古玩,它使用 curl 和 textbelt.com 系统)。我使用了一种过于复杂的方式让用户输入电话号码。我将字符限制为只有数字,自动十个字符长,并自动添加破折号。
这是我的代码:
:start
cls
echo Enter phone number:
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x1=%x% - 1
cls
echo Enter phone number:
echo %x1%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x2=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x3=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x4=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x5=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x6=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x7=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-%x7%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x8=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-%x7%%x8%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x9=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-%x7%%x8%%x9%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x10=%x% - 1
set number=%x1%%x2%%x3%%x4%%x5%%x6%%x7%%x8%%x9%%x10%
cls
echo is this the correct number?
echo. %number%
choice /c yn
set x=%errorlevel%
if %x%==2 goto start
如您所见,它确实又长又复杂。它有效,但它就是您所说的“意大利面条代码”。我敢打赌,有一种更简单的方法可以实现我的目标。
【问题讨论】: