0

基本上,我设置了一个问题,如果我回答“打断”,它应该说“你决定最终走到那里......”,然后在这个问题下,“这个人会有什么反应?” 然后,它应该从 1-21 生成一个随机数,然后暂停。但是,当我回答“中断”时,它会关闭程序。虽然,当我移除随机数生成器时,它会在“这个人将如何反应?”之后暂停工作。我可以退出。数字生成器有什么问题?

这是代码:

echo Do you interrupt or wait until they are finished talking?
echo.
set /p choice=
if %choice%==interrupt (
  echo You decide to finally walk over there. You ask the man who is telling the story, "So what's in the labyrinth?"
  echo How does the man react?
  pause
  set /a num=((20 + 1) * %random%) / 32768 + 1
  echo %num%
  pause
  exit
)

感谢大家阅读和/或回复!

4

4 回答 4

2

如果您想在代码块中读取具有更改值的变量,您总是需要delayed expansion!variables!不是%variables%. 此外,for循环解析器读取代码块内的所有右括号) 并希望结束该块。^您应该使用插入符号或双引号转义此括号。

于 2013-07-14T07:54:29.617 回答
2

你将不得不逃避关闭)

代替

set /a num=((20 + 1) * %random%) / 32768 + 1

set /a num=((20 + 1^) * %random%^) / 32768 + 1
于 2013-07-14T07:57:40.773 回答
0

You have to put quotes around the equation like this

set /a num="((20 + 1) * %random%) / 32768 + 1"

or this

set /a "num=((20 + 1) * %random%) / 32768 + 1"

or as Stephan said, you can also escape the closing brackets ).

Also, if you want to use %num% inside the if "%choice%"=="interrupt" (...) block, you have to use delayed expansion, like Endoro said.

To improve your code, you should also put quotes around the choice like above before comparing it to prevent spaces in %choice% messing with the code.

于 2013-07-14T08:08:32.387 回答
0

下面是一种不需要转义(或使用延迟扩展)的方法,并且还要注意对“中断”进行不区分大小写的检查。

echo Do you interrupt or wait until they are finished talking?
echo.
set /p choice=
if /i not "%choice%"=="interrupt" goto :skip001
  echo You decide to finally walk over there. You ask the man who is telling the story, "So what's in the labyrinth?"
  echo How does the man react?
  pause
  set /a num=((20 + 1) * %random%) / 32768 + 1
  echo %num%
  pause
  exit
:skip001
于 2013-07-14T12:16:34.137 回答