计算批处理脚本中两个变量的总和 [英] Calculating the sum of two variables in a batch script
问题描述
这是我第一次使用Stack Overflow,因此请宽恕这个问题.我一直在尝试对批处理进行编程,并使用DOSbox在我的linux机器上运行它们.
这是我一直在使用的代码:
@echo off
set a=3
set b=4
set c=%a%+%b%
echo %c%
set d=%c%+1
echo %d%
其输出是:
3+4
3+4+1
我该如何添加两个变量而不是回显该字符串?
您需要在set命令上使用属性/a
.
例如
set /a "c=%a%+%b%"
这允许您在set命令中使用算术表达式,而不是简单的串联./p>
您的代码应为:
@set a=3
@set b=4
@set /a "c=%a%+%b%"
echo %c%
@set /a "d=%c%+1"
echo %d%
并输出:
7
8
This is my first time on Stack Overflow so please be lenient with this question. I have been experimenting with programming with batch and using DOSbox to run them on my linux machine.
Here is the code I have been using:
@echo off
set a=3
set b=4
set c=%a%+%b%
echo %c%
set d=%c%+1
echo %d%
The output of that is:
3+4
3+4+1
How would I add the two variables instead of echoing that string?
You need to use the property /a
on the set command.
For example,
set /a "c=%a%+%b%"
This allows you to use arithmetic expressions in the set command, rather than simple concatenation.
Your code would then be:
@set a=3
@set b=4
@set /a "c=%a%+%b%"
echo %c%
@set /a "d=%c%+1"
echo %d%
and would output:
7
8
这篇关于计算批处理脚本中两个变量的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!