Board logo

标题: [ 新手习题 10 ] 批处理四则运算代码改错 [打印本页]

作者: wxcute    时间: 2008-11-20 19:41     标题: [ 新手习题 10 ] 批处理四则运算代码改错

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set A=%random%%%5*9
  4. set B=(%random%%%3+1)*3
  5. set fuhao="+-*/"
  6. :calc
  7. set/a n+=1
  8. set/a RS=A%fuhao:~!n!,1%B
  9. echo A !fuhao:~%n%,1! B = %RS%
  10. if not "!fuhao:~%n%,1!"=="" goto :calc
  11. pause>nul
  12. goto :eof
复制代码
目的:学会分析自己或是他人的代码,改正其中的错误使程序能正常的运行。

要求:修改代码使其显示两个随机数的和差积商。效果如图:
由于是随机数,A、B 不一定是 18 和 9。
[attach]612[/attach]

相关知识点:set 的数学运算表达方式,随机数、变量延迟、变量截取和 goto 配合标签的循环。
作者: lhjoanna    时间: 2008-11-20 23:30

奇怪wxcute兄出的题目很好啊,怎么没人来做。我来做一个:
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set /a A=%random%%%5*9
  4. set /a B=(%random%%%3+1)*3
  5. set "fuhao=+-*/"
  6. set /a n=0
  7. echo A=%A%  B=%B%
  8. :calc
  9. set /a RS=%A%!fuhao:~%n%,1!%B%
  10. echo A !fuhao:~%n%,1! B = !RS!
  11. set /a n+=1
  12. if not "!fuhao:~%n%,1!"=="" goto :calc
  13. pause>nul
  14. goto :eof
复制代码

[ 本帖最后由 lhjoanna 于 2008-11-21 00:14 编辑 ]
作者: stalker    时间: 2008-11-22 21:45

看看高手我的
哈哈,开个玩笑,其实我研究好久才搞明白
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set/a A=%random%%%5*9
  4. set/a B=(%random%%%3+1)*3
  5. set fuhao=+-*/
  6. set/a n=0
  7. :calc
  8. set/a RS=%A%!fuhao:~%n%,1!%B%
  9. echo %A% !fuhao:~%n%,1! %B% = %RS%
  10. set/a n+=1
  11. if not "!fuhao:~%n%,1!"=="" goto :calc
  12. goto :eof
复制代码
wxcute老大,我仍然有个疑问,为什么在变量截取!fuhao:~%n%,1!中只能用%n%而不能是!n!呢?
作者: batman    时间: 2008-11-22 23:12

凑个热闹吧:

  1. @echo off&setlocal enableDelayedExpansion
  2. set /a A=%random%%%5*9,B=(%random%%%3+1)*3
  3. set "fuhao= +-*/"
  4. :calc
  5. set/a n+=1
  6. if "!fuhao:~%n%,1!" equ "" pause>nul&goto :eof
  7. set/a RS=A!fuhao:~%n%,1!B
  8. echo %A% !fuhao:~%n%,1! %B% = %RS%
  9. goto calc
复制代码



[ 本帖最后由 batman 于 2008-11-22 23:13 编辑 ]
作者: stalker    时间: 2008-11-23 11:00

多谢wxcute老大解答我的疑惑(^_^)
作者: h5599859    时间: 2008-11-25 09:43

@echo off
setlocal enableDelayedExpansion
set /a A=%random%%%5*9+1
set /a B=(%random%%%3+1)*3
set fuhao="+-*/
:calc
set/a n+=1
set/a RS=%A%!fuhao:~%n%,1!%B%
echo %A% !fuhao:~%n%,1! %B% = %RS%
if not "!fuhao:~%n%,1!"=="/" goto calc
pause>nul
作者: keen    时间: 2009-3-31 10:50

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set A=%random%%%5*9
  4. set B=(%random%%%3+1)*3
  5. set "fuhao=+-*/"
  6. :calc
  7. if "!fuhao:~%n%,1!"=="" pause &exit
  8. set/a RS=%A%!fuhao:~%n%,1!%B%
  9. echo A !fuhao:~%n%,1! B = %RS%
  10. set /a n+=1 &goto :calc
复制代码

作者: tyc    时间: 2009-3-31 13:44     标题: try

@echo off
setlocal enableDelayedExpansion
set /a A=%random%%%5*9
set /a B=(%random%%%3+1)*3
set fuhao=+-*/a
pause>nul
set /a n=0
:calc
set /a RS=%A% !fuhao:~%n%,1! %B%
echo !A! !fuhao:~%n%,1! !B! = %RS%
set /a n+=1
if not "!fuhao:~%n%,1!"=="a" goto :calc
pause>nul
goto :eof
作者: cainiao736    时间: 2009-3-31 19:49

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set /a A=%random%%%5*9
  4. set /a B=(%random%%%3+1)*3
  5. set fuhao= +-*/
  6. set n=1
  7. :calc
  8. set/a RS=!A! !fuhao:~%n%,1! !B!
  9. echo !A! !fuhao:~%n%,1! !B! = !RS!
  10. set /a n+=1
  11. if not "!fuhao:~%n%,1!"=="" goto :calc
  12. pause>nul
  13. goto :eof
复制代码
嘿嘿,其实不是很明白,改来改去就给改好了
作者: keen    时间: 2009-4-1 09:12

多谢wxcute老大指正!修改如下:
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set /a A=%random%%%5*9
  4. set /a B=(%random%%%3+1)*3
  5. set "fuhao=+-*/"
  6. :calc
  7. if "!fuhao:~%n%,1!"=="" pause &exit
  8. set/a RS=%A%!fuhao:~%n%,1!%B%
  9. echo %A% !fuhao:~%n%,1! %B% = %RS%
  10. set /a n+=1 &goto :calc
复制代码

作者: mckobe    时间: 2009-4-16 15:18

@echo off
setlocal enableDelayedExpansion
set/a A=%random%%%5*9
set/a B=(%random%%%3+1)*3
set fuhao=+-*/
set/a n=0
:calc
if not "!fuhao:~%n%,1!"=="" (set/a RS=%A%!fuhao:~%n%,1!%B%&echo %A% !fuhao:~%n%,1! %B% = !RS!)
set/a n+=1
goto :calc
pause>nul
goto :eof
作者: sjzong    时间: 2009-4-16 22:22     标题: 我也来一个!

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set/a A=%random%%%5*9
  4. set/a B=(%random%%%3+1)*3
  5. echo A=%A% B=%B%
  6. set fuhao=+-*/
  7. :calc
  8. set/a RS=%A% !fuhao:~%n%,1! %B%
  9. echo A !fuhao:~%n%,1! B = !RS!
  10. set/a n+=1
  11. if not "!fuhao:~%n%,1!"=="" goto :calc
  12. pause>nul
  13. goto :eof
复制代码

[ 本帖最后由 sjzong 于 2009-4-16 22:48 编辑 ]
作者: battab    时间: 2013-12-16 14:26

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set /a A=(%random%%%5+1)*9
  4. set /a B=(%random%%%3+1)*3
  5. set fuhao= +-*/
  6. set n=1
  7. :calc
  8. if %n% gtr 4 goto end
  9. set/a RS=A!fuhao:~%n%,1!B
  10. echo %A% !fuhao:~%n%,1! %B% = %RS%
  11. if not "!fuhao:~%n%,1!"=="" set/a n+=1&goto :calc
  12. :end
  13. pause>nul
复制代码

作者: BHsolve    时间: 2013-12-29 20:31

  1. @echo off
  2. color 0B
  3. setlocal enabledelayedexpansion
  4. set /a A="%random%%%5"*9
  5. set /a B=("%random%%%3"+1)*3
  6. echo A=%A% B=%B%
  7. set fuhao=+-*/
  8. set/a n=0
  9. :calc
  10. set/a RS=%A%!fuhao:~%n%,1!%B%
  11. if not "!fuhao:~%n%,1!"=="" (
  12. echo A !fuhao:~%n%,1! B = %RS%
  13. set /a n+=1 && goto calc
  14. )
  15. pause>nul
复制代码
学习了
作者: ann    时间: 2014-12-20 15:20

谢谢了,第一次遇到这样的题,学习了
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set /a A=%random%%%5*9
  4. set /a B=(%random%%%3+1)*3
  5. set fuhao="-+*/"
  6. :calc
  7. set /a n+=1
  8. set /a RS=A!fuhao:~%n%,1!B
  9. echo %A% !fuhao:~%n%,1! %B% = %RS%
  10. if not "%n%"=="4" goto :calc
  11. pause>nul
  12. goto :eof
复制代码

作者: 尘丶    时间: 2015-5-31 09:41

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. set /a A=%random%%%5*9
  4. set /a B=(%random%%%3+1)*3
  5. set fuhao="+-*/"
  6. :calc
  7. set/a n+=1
  8. set/a RS=%A%!fuhao:~%n%,1!%B%
  9. echo %A% !fuhao:~%n%,1! %B% = %RS%
  10. if %n%==4 goto exit
  11. if not "!fuhao:~%n%,1!"=="" goto :calc
  12. pause>nul
  13. goto :eof
  14. :exit
  15. pause
复制代码
这题算是10吗,比之前的for处理容易多了 = =
作者: 437153    时间: 2015-6-8 11:24

  1. @echo off&setlocal enabledelayedexpansion
  2. set/a A=%random%%%5*9
  3. set/a B=(%random%%%3+1)*3
  4. set "fuhao=+-*/"
  5. :calc
  6. set/a RS=A!fuhao:~%n%,1!B
  7. echo %A% !fuhao:~%n%,1! %B% = %RS%
  8. set/a n+=1
  9. if not "!fuhao:~%n%,1!" == "" goto :calc
  10. pause>nul
  11. goto :eof
复制代码

作者: m91opse    时间: 2018-5-5 19:44

大哥这么厉害,求大哥,帮我解决这个问题吧 http://www.bathome.net/thread-47992-1-1.html




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2