批处理字符串中的空格 [英] spaces in Batch strings

查看:124
本文介绍了批处理字符串中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个随机附魔武器发生器.但是,当随机选择带有空格的附魔时,批处理文件偶尔会关闭.

I want to create a random enchanted weapon generator. However, every once in a while the batch file closes when an enchantment with a space is randomly selected.

我已经尝试过将字符串用引号引起来,但是当我使用变量显示字符串时,该字符串带有引号.例如:

I've already tried putting the string in quotes, but then when I use a variable to display the string, the string has the quotation marks. For Example:

if %enchantment% == 1 set enchantment="Health Absorbing"

但是当我显示%结界%时,它的出现是吸收健康"而不是吸收健康.

But then when I display %enchantment% it shows up with "Health Absorbing" rather than Health Absorbing.

我也尝试过将变量放在引号中,但这似乎无法解决问题.

I've also tried putting the variable in quotes as well, but that doesn't seem to fix the problem.

if %enchantment% == 30 set "enchantment=Health Absorbing"

到目前为止,这是我的代码的较小版本:

Here's a smaller version of my code so far:

@echo off

:Weapon
set /a weapon=%random% * 3 / 32768 + 1
if %weapon% == 1 set weapon=Battleaxe
if %weapon% == 2 set weapon=Sword
if %weapon% == 3 set weapon=Mace

:Enchant
set /a enchantmenttype=%random% * 2 / 32768 + 1
if %enchantmenttype% ==1 goto Enchant1
if %enchantmenttype% ==2 goto Enchant2

:Enchant1
set /a enchantment=%random% * 3 / 32768 + 1
if %enchantment% == 1 set enchantment=Flaming
if %enchantment% == 2 set enchantment=Holy
if %enchantment% == 3 set enchantment=Frozen
set "EW=%enchantment% %weapon%"
goto EW

:Enchant2
set /a enchantment=%random% * 3 / 32768 + 1
if %enchantment% == 1 set "enchantment=the Fire Spirit"
if %enchantment% == 2 set "enchantment=the Frozen Spirit"
if %enchantment% == 3 set "enchantment=the Phantom Spirit"
set "EW=%weapon% of %enchantment%"
goto EW

:EW
echo %EW%
pause >nul
goto Weapon

我希望代码能够在每次按下按钮时显示带有随机武器的随机结界.但是,该代码只会在意外关闭之前执行1-10次.

I want the code to be able to display a random enchantment with a random weapon every time you press a button. However, the code only does this 1-10 times before it unexpectedly shuts down.

推荐答案

在比较的两边都使用引号.这样,变量的内容将被视为单个项目.但是,您必须在 == 的另一侧包括引号,因为比较中包括了引号.

Use quotes on both sides of your comparisons. This way, the contents of the variable gets treated as a single item. However, you have to include the quotes on the other side of the == because the quotes are included in the comparison.

@echo off

:Weapon
set /a weapon=%random% %% 3 + 1
if "%weapon%"=="1" set weapon=Battleaxe
if "%weapon%"=="2" set weapon=Sword
if "%weapon%"=="3" set weapon=Mace

:Enchant
set /a enchantmenttype=%random% %% 2 + 1
if "%enchantmenttype%"=="1" goto Enchant1
if "%enchantmenttype%"=="2" goto Enchant2

:Enchant1
set /a enchantment=%random% %% 3 + 1
if "%enchantment%"=="1" set enchantment=Flaming
if "%enchantment%"=="2" set enchantment=Holy
if "%enchantment%"=="3" set enchantment=Frozen
set "EW=%enchantment% %weapon%"
goto EW

:Enchant2
set /a enchantment=%random% %% 3 + 1
if "%enchantment%"=="1" set "enchantment=the Fire Spirit"
if "%enchantment%"=="2" set "enchantment=the Frozen Spirit"
if "%enchantment%"=="3" set "enchantment=the Phantom Spirit"
set "EW=%weapon% of %enchantment%"
goto EW

:EW
echo %EW%
pause >nul
goto Weapon

这篇关于批处理字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆