[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
"!res:%res:~,1%=!"==""  这句怎么理解啊?

TOP

本帖最后由 悬崖之树 于 2012-8-5 16:23 编辑

回复 5# xhhivi


判断是否相同试试下面的:
  1. @echo off
  2. set a=0000 1111 2222 3333 4444 5555 6666 7777 8888 9999
  3. :ks
  4. set /p b=请输入一个四位数:
  5. for %%i in (%a%) do (
  6. if %b%==%%i echo 数字相同请重新输入!& goto ks
  7. )
  8. echo 你输入的数符合要求!再来一次吧!& goto ks
复制代码
但是如果你输入的不是四位数的话,也将显示“你输入的数符合要求!再来一次吧!”

TOP

回复 3# 601997526
为什么有的数字会报错呢?因为代码不完善,应写成:
  1. @echo off & setlocal enabledelayedexpansion
  2. :begin
  3. set n=1
  4. set /a res=%random%%%9000+1000
  5. echo 初始数是 %res%
  6. if "!res:%res:~,1%=!"=="" echo %res% 是全相同数&pause&goto begin
  7. :lp
  8. set "str="&set "str1="
  9. for /l %%i in (0 1 3) do echo !res:~%%i,1!>>tmp.txt
  10. for /f %%i in ('sort tmp.txt') do set "str=%%i!str!"&set "str1=!str1!%%i"
  11. set /a a=%str%-(1%str1%-10000)
  12. set b=000%a%
  13. set res=%b:~-4%
  14. echo %str%-%str1%=%res%
  15. del tmp.txt
  16. if %res% neq 6174 set /a n+=1&goto lp
  17. echo 共运行了%n%次
  18. pause & cls & goto begin
复制代码
这样就不会出错了!

TOP

下面这个是手动的,自己输入"7877" 和"6766" 试试吧!还可以输入类似 0001 0101 之类的。
  1. @echo off & setlocal enabledelayedexpansion
  2. :begin
  3. set n=1
  4. set /p res=请输入一个四位数,每位数字是0~9,但不能完全相同:
  5. if "!res:%res:~,1%=!"=="" echo %res% 是全相同数&pause&goto begin
  6. :lp
  7. set "str="&set "str1="
  8. for /l %%i in (0 1 3) do echo !res:~%%i,1!>>tmp.txt
  9. for /f %%i in ('sort tmp.txt') do set "str=%%i!str!"&set "str1=!str1!%%i"
  10. set /a a=%str%-(1%str1%-10000)
  11. set b=000%a%
  12. set res=%b:~-4%
  13. echo %str%-%str1%=%res%
  14. del tmp.txt
  15. if %res% neq 6174 set /a n+=1&goto lp
  16. echo 共运行了%n%次
  17. pause & cls & goto begin
复制代码

TOP

本帖最后由 悬崖之树 于 2012-8-10 22:19 编辑

五位数的黑洞是什么呢?
运行下面的就知道了:
  1. @echo off & setlocal enabledelayedexpansion
  2. :begin
  3. set /p res=请输入一个五位数,每位数字是0~9,但不能完全相同:
  4. if "!res:%res:~,1%=!"=="" echo %res% 是全相同数&pause&goto begin
  5. :lp
  6. set "str="&set "str1="
  7. for /l %%i in (0 1 4) do echo !res:~%%i,1!>>tmp.txt
  8. for /f %%i in ('sort tmp.txt') do set "str=%%i!str!"&set "str1=!str1!%%i"
  9. set /a a=%str%-(1%str1%-100000)
  10. set b=0000%a%
  11. set res=%b:~-5%
  12. echo %str%-%str1%=%res%
  13. echo %str%-%str1%=%res%>>2.txt
  14. del tmp.txt
  15. goto lp
复制代码

TOP

返回列表